使用python 3.2.2 tkinter我创建了一个带有滚动条的画布,但是当我移动滚动条时,画布内的数据不滚动 [英] Using python 3.2.2 tkinter I create a canvas with scrollbars attached but the data inside the canvas doesn't scroll when I move the scrollbars

查看:1359
本文介绍了使用python 3.2.2 tkinter我创建了一个带有滚动条的画布,但是当我移动滚动条时,画布内的数据不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python 3.2.2 tkinter我创建一个带有滚动条的画布,但是当我移动滚动条时,画布内的数据不滚动。与tkinter的语法问题或错误?

Using python 3.2.2 tkinter I create a canvas with scrollbars attached but the data inside the canvas doesn't scroll when I move the scrollbars. Syntax issue or bug with tkinter?

示例代码:

from tkinter import *

## GUI color map ###
black = "#000000"

root = Tk()
root.minsize(100,100)
root.maxsize(500,500)
root.resizable(0,0)

topFrame = Frame(root, bd=2, relief=SUNKEN)
topFrame.pack()

xscrollbar = Scrollbar(topFrame, orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM, fill=X)

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

conFrame = Canvas(topFrame, bd=0, scrollregion=(0, 0, 1000, 1000),
                xscrollcommand=xscrollbar.set,
                yscrollcommand=yscrollbar.set)

conFrame.pack(side=TOP, fill=BOTH, expand=1)
xscrollbar.config(command=conFrame.xview)
yscrollbar.config(command=conFrame.yview)

for i in range(1,30):
    function_name = 'my'+str(i)+'ItemList = StringVar()'
    exec(function_name)
    function_name = 'my'+str(i)+'Item = '+str(i)
    exec(function_name)
    function_name = 'my'+str(i)+'ItemList.set(my'+str(i)+'Item)'
    exec(function_name)
    function_name = 'my'+str(i)+'ItemListEntry = Entry(conFrame, width=148,     foreground=black, textvariable=my'+str(i)+'ItemList)'
    exec(function_name)
    function_name = 'my'+str(i)+'ItemListEntry.pack(side=TOP, padx=2)'
    exec(function_name)

mainloop()


推荐答案

滚动条您正在创建的窗口小部件(以非常不寻常的方式)不属于画布,因此它们不会滚动。如果在画布上使用绘图命令,您将看到画布项目实际上是滚动的。

The scrollbars do scroll the canvas. The widgets you are creating (in a highly unusual fashion) are not part of the canvas, so they don't scroll. If you use the drawing commands on the canvas you'll see that the canvas items do in fact scroll.

为了滚动窗口小部件,您必须使用 create_window 将它们嵌入到画布中。如果你想创建一个滚动框架,典型的解决方案是把所有的窗口部件放在框架中,使用 create_window 将该框架添加到画布,然后设置 scrollregion 到框架的大小(你必须在屏幕更新后做,因为尺寸不计算,直到小部件实际呈现)

In order for the widgets to scroll, you must embed them in the canvas with create_window. If you're trying to create a scrolling frame, the typical solution is to put all your widgets in a frame, use create_window to add that frame to the canvas, then set the scrollregion to the size of the frame (which you have to do after a screen update since the size isn't calculated until the widget is actually rendered)

在旁注,你为什么要使用exec?它使你的代码非常难以理解,你得不到回报的好处。你知道你可以用更简单的东西替换这10行吗?例如:

On a side note, why are you using exec? It makes your code extremely hard to understand, and you get no benefit in return. Are you aware you can replace those 10 lines with something considerably more simple? For example:

var = {}
for i in range(1,30):
    var[i] = StringVar()
    var[i].set(i)
    e = Entry(conFrame, width=148, foreground=black, textvariable=var[i])
    e.pack(side=TOP, padx=2)

这篇关于使用python 3.2.2 tkinter我创建了一个带有滚动条的画布,但是当我移动滚动条时,画布内的数据不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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