Make tkinter text widget fit to window

徘徊边缘 提交于 2019-12-24 13:18:59

问题


I'm making a text editor whose main widget is a Text widget for the user to actually enter text. I need to make the text widget fit to the window when the user resizes the pane. I kind of cheated by making the widget huge, but that's just a makeshift solution to let me work on other parts while I look for a solution. How can I make the Text widget automatically resize to fit the window?


回答1:


Use pack to place the widget, with expand set to True and fill set to BOTH. Ex:

    from tkinter import *
    root=Tk()

    text=Text(root)
    text.pack(expand=True, fill=BOTH)

    root.mainloop



回答2:


Same example, but now with a scrollbar on the right side:

from Tkinter import *
root = Tk()
S = Scrollbar(root)
T = Text(root)
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, expand=True, fill=BOTH)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
quote = """asjkdahskdja
asjkkdasjk \r
sadjshadjksd\n
asjhdasdka\t\sdasd\taasdasd\t."""
T.insert(END, quote)


来源:https://stackoverflow.com/questions/29777630/make-tkinter-text-widget-fit-to-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!