在Tkinter的Canvas中的scrollregion?

时间:2012-08-08 07:53:09

标签: canvas python-3.x tkinter scrollbar

我用Python编写了一个程序。但我想知道是否可能有更短的方法。特别是关于“Canvas”,“Frame”,“LabelFrame”和“Label”,还有其他任何方法可以自动制作“scrollregion”(而不是我写的,如果可能的话,在Tkinter自然的方式)

以下是代码:

from tkinter import *
dict = {}
Liste = ["bir","iki","üç","4",5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
a = 1

####### ekleme basi ##################################################
pencere = Tk()
pencere.geometry("{0}x{1}".format(100, 100))

labelFrame = LabelFrame(pencere, width = 100, height = 100)
labelFrame.pack(side="left",fill=BOTH)
canvas = Canvas(labelFrame,relief=SUNKEN)
#canvas.config(scrollregion=(0,0,300,1000))    # halloldu =)

scrollbar = Scrollbar(labelFrame)
scrollbar.config(command=canvas.yview)
canvas.config(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right",fill=Y)
canvas.pack(side="left",expand=YES,fill=BOTH)

frame = Frame(canvas,width=100,height=100)
frame.pack(side="top",fill=BOTH)
canvas.create_window(0,0,window=frame, anchor="nw")
####### ekleme sonu ##################################################

bas = [0, 0, 0, 100]
for i in Liste:
    dict["a%s"%a] = Label(frame, text=i)    # "frame" ekledim
    dict["a%s"%a].grid(row=a)
    canvas["scrollregion"] = "%d %d %d %d" %(bas[0], bas[1], bas[2], bas[3])
    bas[3] += 16    # "scrollregion" i dinamik olarak arttir (degistirebilirsiniz)
    a += 1


mainloop() 

注意:英语是我的第二语言,我不太了解,很抱歉。

1 个答案:

答案 0 :(得分:3)

设置滚动区域的典型方法是将其设置为画布中所有内容的边界框,可以使用bbox方法检索它,并为其提供文字字符串"all"

canvas.configure(scrollregion = canvas.bbox("all"))