Bou*_*Dev 3 python user-interface tkinter python-3.x
我想使用scrolledtext模块创建一个ScrolledText小部件,以便在python中创建GUI。我已经成功创建了 ScrolledText 小部件,但是我无法向其添加水平滚动条。
e3=ScrolledText(window3,width=24,height=13)
e3.grid(row=5,column=1,rowspan=6,columnspan=4)
Run Code Online (Sandbox Code Playgroud)
上面的代码片段用于创建 ScrolledText 小部件。
该小部件仅包含垂直滚动条。我也想为其添加一个水平滚动条。知道怎么做吗?
更新:ScrolledText 小部件用于接受多行输入。检查所附图片。
您可以使用文本小部件和几个滚动条自己实现滚动文本小部件。
下面的示例将一个文本小部件和两个滚动条放在一个框架中,以便它们看起来就像是单个小部件。这几乎正是该ScrolledText小部件的作用:
import tkinter as tk
root = tk.Tk()
textContainer = tk.Frame(root, borderwidth=1, relief="sunken")
text = tk.Text(textContainer, width=24, height=13, wrap="none", borderwidth=0)
textVsb = tk.Scrollbar(textContainer, orient="vertical", command=text.yview)
textHsb = tk.Scrollbar(textContainer, orient="horizontal", command=text.xview)
text.configure(yscrollcommand=textVsb.set, xscrollcommand=textHsb.set)
text.grid(row=0, column=0, sticky="nsew")
textVsb.grid(row=0, column=1, sticky="ns")
textHsb.grid(row=1, column=0, sticky="ew")
textContainer.grid_rowconfigure(0, weight=1)
textContainer.grid_columnconfigure(0, weight=1)
textContainer.pack(side="top", fill="both", expand=True)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)