简体   繁体   English

特金特。 滚动条在画布上不起作用

[英]Tkinter. Scrollbar is not working on Canvas

I am newbie in programming, don't hate me pls :)我是编程新手,请不要恨我 :)

Why scroll is not working on my canvas widget?为什么滚动在我的画布小部件上不起作用? I added loop with 30 rows and I cannot scroll down.我添加了 30 行的循环,但无法向下滚动。 Its look like it because of create_text() method or maybe not.它看起来像因为 create_text() 方法或可能不是。 I've written code for example below.我已经编写了下面的示例代码。

from tkinter import *

root = Tk()
root.geometry('200x150')
frame = Frame(root)

yscrollbar = Scrollbar(frame, orient=VERTICAL)
yscrollbar.pack(fill=Y, side=RIGHT)

canvas = Canvas(frame,
                yscrollcommand=yscrollbar.set,
                bg='white')
canvas.pack(fill=BOTH)
yscrollbar.config(command=canvas.yview)

n=12
for i in range(1,31):
    canvas.create_text(10,n,text=i)
    n+=12

frame.pack()
root.mainloop()

我的代码

Scrolling is not responsive because you need to tell the canvas to limit the scrolling to a given area.滚动没有响应,因为您需要告诉画布将滚动限制在给定区域。

You can use the bbox method to get a bounding box for a given object, or a group of objects.您可以使用bbox方法获取给定对象或一组对象的边界框。

canvas.bbox(ALL) returns the bounding box for all objects on the canvas. canvas.bbox(ALL)返回画布上所有对象的边界框。

Link: http://effbot.org/zone/tkinter-scrollbar-patterns.htm you can check other methods to do this in this link链接: http : //effbot.org/zone/tkinter-scrollbar-patterns.htm您可以在此链接中查看其他方法来执行此操作

Here is the working code:这是工作代码:

from tkinter import *

root = Tk()
root.geometry('200x150')
frame = Frame(root)

yscrollbar = Scrollbar(frame, orient=VERTICAL)
yscrollbar.pack(fill=Y, side=RIGHT)

canvas = Canvas(frame,
                yscrollcommand=yscrollbar.set,
                bg='white')
canvas.pack(fill=BOTH)
yscrollbar.config(command=canvas.yview)

n=12
for i in range(1,31):
    canvas.create_text(10,n,text=i)
    n+=12

frame.pack()

# Add this line to tell the canvas the area over to scroll
canvas.config(scrollregion=canvas.bbox(ALL))

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM