如何在Tkinter中获得带有滚动条的框架? [英] How could I get a Frame with a scrollbar in Tkinter?

查看:329
本文介绍了如何在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:

This widget is used to implement scrolled listboxes, canvases, and text fields.

是否可以将ScrollbarFrame一起使用?

Is there a way to use the Scrollbar with a Frame?

推荐答案

如果可以使用Tix,则可以使用ScrolledWindow小部件,它具有window框架和一个或两个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事件中添加代码,以便在通过键盘切换时滚动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小部件(可以添加Label,Entry和Text子部件)并自己编写更多代码以实现所需的功能.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆