如何在 Tkinter 中获得带有滚动条的框架? [英] How could I get a Frame with a scrollbar in Tkinter?
问题描述
我想要一个 Frame
,用户可以在其中根据应用程序的需要添加尽可能多的文本字段.
I'd like to have a Frame
, where the user could add as many textfields as needed by the application.
应用程序以一个文本字段和该文本字段下方的按钮开始.当用户按下按钮时,将在第一个下方添加一个新的文本条目(这可能会重复无数次).在窗口中间,会有一个 Text
小部件,用于显示文本 :)
The application starts with a textfield, and a button below that textfield. When the user presses the button, a new text entry will be added below the first one ( this may be repeated countless times ). In the middle of the window, there will be a Text
widget, used to display text :)
但是,我在文档中注意到了这一点:
However, I noticed this in the documentation:
此小部件用于实现滚动列表框、画布和文本字段.
有没有办法将 Scrollbar
与 Frame
一起使用?
Is there a way to use the Scrollbar
with a Frame
?
推荐答案
如果你可以使用 Tix,有一个 ScrolledWindow 小部件,它有一个 window
Frame 和一两个 Scrollbar 小部件:
If you can use Tix, there is the ScrolledWindow widget which has a window
Frame and one or two Scrollbar widgets:
import Tix as tk
r= tk.Tk()
r.title("test scrolled window")
sw= tk.ScrolledWindow(r, scrollbar=tk.Y) # just the vertical scrollbar
sw.pack(fill=tk.BOTH, expand=1)
for i in xrange(10):
e= tk.Entry(sw.window)
e.pack()
r.mainloop()
改变根窗口的大小.您需要向 Entry 小部件的 focus_get 事件添加代码,以便在通过键盘进行 Tab 键切换时滚动 ScrolledWindow.
Change the size of the root window. You will want to add code to the focus_get event of the Entry widgets to scroll the ScrolledWindow when tabbing through the keyboard.
否则,您将不得不使用 Canvas 小部件(您可以添加标签、条目和文本子小部件)并自己编写更多代码来实现您所需的功能.
Otherwise, you will have to use a Canvas widget (you can add Label, Entry and Text subwidgets) and write a lot more code yourself to implement your required functionality.
这篇关于如何在 Tkinter 中获得带有滚动条的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!