如何为文本小部件添加滚动条?

0 投票
1 回答
1219 浏览
提问于 2025-04-17 13:14

如何给文本小部件添加滚动条,如果这个文本小部件是在顶层窗口里,并且是通过网格布局管理器添加的。

我的意思是我在“顶层”窗口/对话框里得到了这个:

ttk.Label(toplevel,text="Text Area").grid(row=8,sticky=E)
self.TextAreaCCOrder=Text(toplevel,height=10,width=50 ).grid(row=8,column=1)

PS:我还是个新手 :)

1 个回答

0

这里有一个例子,它创建了一个带滚动条的框架和一个文本小部件:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        # create the text and scrollbar widgets
        text = tk.Text(self, wrap="word")
        vsb = tk.Scrollbar(self, orient="vertical")

        # connect them to each other
        text.configure(yscrollcommand=vsb.set)
        vsb.configure(command=text.yview)

        # use grid to arrange the widgets (though pack is simpler if
        # you only have a single scrollbar)
        vsb.grid(row=0, column=1, sticky="ns")
        text.grid(row=0, column=0, sticky="nsew")

        # configure grid such that the cell containing the text
        # widget grows and shrinks with the window
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

if __name__ == "__main__":
    root = tk.Tk()
    frame = Example(parent=root)
    frame.pack(side="top", fill="both", expand=True)

    root.mainloop()

撰写回答