python 如何在 Tkinter 中获得带有滚动条的框架?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1873575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:14:51  来源:igfitidea点击:

How could I get a Frame with a scrollbar in Tkinter?

pythonscrollbartkinterframe

提问by Geo

I'd like to have a Frame, where the user could add as many textfields as needed by the application.

我想要一个Frame,用户可以根据应用程序的需要添加尽可能多的文本字段。

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 Textwidget, used to display text :)

该应用程序以一个文本字段和该文本字段下方的按钮开始。当用户按下按钮时,将在第一个下方添加一个新的文本条目(这可能会重复无数次)。在窗口中间,会有一个Text小部件,用于显示文本:)

However, I noticed this in the documentation:

但是,我在文档中注意到了这一点:

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

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

Is there a way to use the Scrollbarwith a Frame?

有没有办法使用Scrollbarwith Frame

回答by tzot

If you can use Tix, there is the ScrolledWindow widget which has a windowFrame and one or two Scrollbar widgets:

如果您可以使用 Tix,则可以使用 ScrolledWindow 小部件,它有一个windowFrame 和一两个 Scrollbar 小部件:

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()

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.

更改根窗口的大小。您需要向 Entry 小部件的 focus_get 事件添加代码,以便在通过键盘进行 Tab 键切换时滚动 ScrolledWindow。

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.

否则,您将不得不使用 Canvas 小部件(您可以添加标签、条目和文本子小部件)并自己编写更多代码来实现您所需的功能。

回答by reckoner

The following is an example of auto hiding scrollbarsthat only works if you just use the gridgeometry manager, taken from the effbot.orgdocumentation:

以下是自动隐藏滚动条的示例,仅当您仅使用网格几何管理器时才有效,取自effbot.org文档:

from tkinter import *


class AutoScrollbar(Scrollbar):
    # A scrollbar that hides itself if it's not needed.
    # Only works if you use the grid geometry manager!
    def set(self, lo, hi):
        if float(lo) <= 0.0 and float(hi) >= 1.0:
            # grid_remove is currently missing from Tkinter!
            self.tk.call("grid", "remove", self)
        else:
            self.grid()
        Scrollbar.set(self, lo, hi)
    def pack(self, **kw):
        raise TclError("cannot use pack with this widget")
    def place(self, **kw):
        raise TclError("cannot use place with this widget")


# create scrolled canvas

root = Tk()

vscrollbar = AutoScrollbar(root)
vscrollbar.grid(row=0, column=1, sticky=N+S)
hscrollbar = AutoScrollbar(root, orient=HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky=E+W)

canvas = Canvas(root, yscrollcommand=vscrollbar.set, xscrollcommand=hscrollbar.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)

vscrollbar.config(command=canvas.yview)
hscrollbar.config(command=canvas.xview)

# make the canvas expandable
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

# create canvas contents
frame = Frame(canvas)
frame.rowconfigure(1, weight=1)
frame.columnconfigure(1, weight=1)

rows = 5
for i in range(1, rows):
    for j in range(1, 10):
        button = Button(frame, text="%d, %d" % (i,j))
        button.grid(row=i, column=j, sticky='news')

canvas.create_window(0, 0, anchor=NW, window=frame)
frame.update_idletasks()
canvas.config(scrollregion=canvas.bbox("all"))

root.mainloop()