简体   繁体   English

添加标签后,滚动条在tkinter画布小部件中不起作用

[英]Scrollbar does not work in tkinter canvas widget after adding labels

I have mad an app that ads label to the canvas. 我为一个广告贴在画布上的应用疯狂。 I have also set the vertical scrollbar. 我还设置了垂直滚动条。 The problem is that I can not activate scrollbar, the scrollbar does not allow scrolling. 问题是我无法激活滚动条,该滚动条不允许滚动。 I have used grid_propagate(0) but it still does not work. 我使用了grid_propagate(0)但仍然无法正常工作。 HOw to activate this feature? 如何激活此功能? The program does not show me any error. 该程序没有显示任何错误。

from tkinter import *

root=Tk()
frame=Frame(root,width=300,height=300)
frame.grid(row=0,column=0)
frame.grid_propagate(0)

canvas=Canvas(frame,width=300,height=300)

vbar=Scrollbar(frame,orient=VERTICAL)
vbar.grid(row=0, column=1,sticky="NS")
vbar.config(command=canvas.yview)

canvas.config(width=300,height=300)
canvas.config(yscrollcommand=vbar.set)
canvas.grid(row=0,column=0)

n=0
m=10
def novi():
    global n
    global m

    n +=1

    m=m+2

    e = Label(canvas, text=m )
    e.grid(row=n,column=0, padx=10, pady=10)

b = Button(root, text="add", command=novi)
b.grid(row=0, column=2,sticky="N")

root.mainloop()

You can try doing something like this: 您可以尝试执行以下操作:

  1. Indicate the scrollregion of the canvas, example: 指示画布的滚动区域,例如:

    canvas.config(scrollregion=(0,0, 940, 1200)) canvas.config(scrollregion = [0,0,940,1200))

  2. Indicate where to put your widget inside the canvas with a code similar to: 使用类似以下代码来指示将小部件放在画布内的位置:

    canvas.create_window(x, y, window=here_goes_your_widget) canvas.create_window(x,y,window = here_goes_your_widget)

    In this example 'x' and 'y' are the coordinates. 在此示例中,“ x”和“ y”是坐标。


As far as I know you must to manually indicate the position of the widget you want to put inside your canvas. 据我所知,您必须手动指示要放在画布中的小部件的位置。

Well, I hope this serves as a help. 好吧,我希望这能对您有所帮助。 Hasta la próxima, che! Hasta lapróxima,che!

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

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