Tkinter、Python 中框架的垂直滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40526496/
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-08-19 23:37:25  来源:igfitidea点击:

Vertical scrollbar for frame in Tkinter, Python

pythonpython-3.xtkinterscrollbartkinter-canvas

提问by Luke25361

My aim is to have a scrollbar that stays at the right-side of a full-screen window, allowing the user to scroll up and down through various different widgets (such as labels & buttons). From other answers I've seen on this site, I've come to the conclusion that a scrollbar has to be assigned to a canvas in order for it to function properly, which I have tried to include in my code but have not had much success with.

我的目标是让滚动条停留在全屏窗口的右侧,允许用户上下滚动各种不同的小部件(例如标签和按钮)。从我在本网站上看到的其他答案中,我得出的结论是,必须将滚动条分配给画布才能使其正常运行,我曾尝试将其包含在我的代码中,但没有太多成功与。

The below code shows a simplified version of what I've managed to accomplish so far:

下面的代码显示了我迄今为止设法完成的简化版本:

from tkinter import *
root = Tk()
root.state("zoomed")
root.title("Vertical Scrollbar")
frame = Frame(root)
canvas = Canvas(frame)
Label(canvas, text = "Test text 1\nTest text 2\nTest text 3\nTest text 4\nTest text 5\nTest text 6\nTest text 7\nTest text 8\nTest text 9", font = "-size 100").pack()
scrollbar = Scrollbar(frame)
scrollbar.pack(side = RIGHT, fill = Y)
canvas.configure(yscrollcommand = scrollbar.set)
canvas.pack()
frame.pack()
root.mainloop()

I'm facing two issues when running this code:

运行此代码时,我面临两个问题:

One is that the scrollbar is inactive, and doesn't allow for the user to scroll down to view the rest of the text.

一种是滚动条处于非活动状态,并且不允许用户向下滚动以查看文本的其余部分。

The other is that the scrollbar is attached to the right-side of the text, rather than the right-side of the window.

另一种是滚动条附加在文本的右侧,而不是窗口的右侧。

So far, none of the other answers I've found on this site have enabled me to amend my code to support a fully-functioning scrollbar for my program. I'd be very grateful for any help anyone reading this could provide.

到目前为止,我在这个站点上找到的其他答案都没有使我能够修改我的代码以支持我的程序的全功能滚动条。我将非常感谢阅读本文的任何人可以提供的任何帮助。

回答by furas

See again link: https://stackoverflow.com/a/3092341/7432

再看链接:https: //stackoverflow.com/a/3092341/7432

It shows how to create scrolled frame - and then you can add all widgets in this frame.

它展示了如何创建滚动框架 - 然后您可以在此框架中添加所有小部件。

import tkinter as tk


def on_configure(event):
    # update scrollregion after starting 'mainloop'
    # when all widgets are in canvas
    canvas.configure(scrollregion=canvas.bbox('all'))


root = tk.Tk()

# --- create canvas with scrollbar ---

canvas = tk.Canvas(root)
canvas.pack(side=tk.LEFT)

scrollbar = tk.Scrollbar(root, command=canvas.yview)
scrollbar.pack(side=tk.LEFT, fill='y')

canvas.configure(yscrollcommand = scrollbar.set)

# update scrollregion after starting 'mainloop'
# when all widgets are in canvas
canvas.bind('<Configure>', on_configure)

# --- put frame in canvas ---

frame = tk.Frame(canvas)
canvas.create_window((0,0), window=frame, anchor='nw')

# --- add widgets in frame ---

l = tk.Label(frame, text="Hello", font="-size 50")
l.pack()

l = tk.Label(frame, text="World", font="-size 50")
l.pack()

l = tk.Label(frame, text="Test text 1\nTest text 2\nTest text 3\nTest text 4\nTest text 5\nTest text 6\nTest text 7\nTest text 8\nTest text 9", font="-size 20")
l.pack()

# --- start program ---

root.mainloop()