Tkinter帆布&滚动条 [英] Tkinter Canvas & Scrollbar

查看:199
本文介绍了Tkinter帆布&滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了与此相关的几个问题和答案,但是没有一个解决方案使我明白为什么此代码不起作用:

I have found several questions and answers related to this but none of the solutions make me understand why this code doesn't work:

root=tk.Tk()

vscrollbar = tk.Scrollbar(root)

c= tk.Canvas(root,background = "#D2D2D2",yscrollcommand=vscrollbar.set)

vscrollbar.config(command=c.yview)
vscrollbar.pack(side=tk.LEFT, fill=tk.Y) 

f=tk.Frame(c) #Create the frame which will hold the widgets

c.pack(side="left", fill="both", expand=True)
c.create_window(0,0,window=f)

testcontentA = tk.Label(f,wraplength=350 ,text=r"Det er en kendsgerning, at man bliver distraheret af læsbart indhold på en side, når man betragter dens websider, som stadig er på udviklingsstadiet. Der har været et utal af websider, som stadig er på udviklingsstadiet. Der har været et utal af variationer, som er opstået enten på grund af fejl og andre gange med vilje (som blandt andet et resultat af humor).")
testcontentB = tk.Button(f,text="anytext")
testcontentA.pack()
testcontentB.pack()

f.pack()

c.config(scrollregion=c.bbox("all"))

root.mainloop()

我有一个tkinter窗口,该窗口具有动态生成的内容-这意味着如果要在屏幕上显示两个小部件,则需要能够向下滚动.我希望能够用小部件填充框架'f',然后使用canvas.create_window()函数以可滚动的方式显示它.

I have a tkinter window which has dynamically generated content - which means need to be able to scroll down if there are two many widgets to be displayed on screen. I hope to be able to populate the frame 'f' with the widgets and then use the canvas.create_window() function to display it in a scrollable way.

现在显示窗口,但未启用滚动条-就像滚动区域不正确,或者滚动条和画布之间的链接不正确.

Right now the window is displayed but the scrollbar is not enabled - its like the scrollregion is not correct or the link between the scrollbar and the canvas is not correct.

推荐答案

如果要在画布中嵌入框架,则必须使用create_window,而不是pack.第一步是删除f.pack().

If you are embedding a frame in a canvas you must use create_window, not pack. The first step is to remove f.pack().

第二,您需要在tkinter有机会显示小部件之后,设置bbox ,因为在实际将它们绘制到屏幕上之前,无法计算它们的大小.

Second, you need to set the bbox after tkinter has had a chance to display the widgets since their size cannot be computed until they are actually drawn on the screen.

一种简单的实现方法是在调用c.config(scrollregion=c.bbox("all"))之前先调用root.update(),或者安排该命令与after_idle一起运行.

A simple way to accomplish that is to call root.update() before calling c.config(scrollregion=c.bbox("all")), or schedule that command to be run with after_idle.

代码中的另一个错误(我假设...)是您在(0,0)处创建了窗口,但未设置锚点.这意味着当我猜测您希望左上角位于(0,0)时,内部框架将以(0,0)为中心.

Another bug in your code (I'm assuming...) is that you create the window at (0,0) but don't set the anchor. That means that the internal frame will be centered at (0,0) when I'm guessing you want the upper left corner to be at (0,0).

更改您的create_window调用,使其看起来像这样:

Change your create_window call to look like this:

c.create_window(0,0,window=f, anchor='nw')

这篇关于Tkinter帆布&滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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