滚动条无法正常运行Tkinter

时间:2014-03-16 14:41:20

标签: python python-2.7 tkinter scrollbar

我是Python的新手。

我正在尝试创建一个滚动条,我做了但滚动条功能不正常。

框架已经显示了所有标签,但我期望的是框架最多只能显示5个标签

如果框架包含超过5个标签,则滚动条将用于显示看不见的剩余标签。

任何帮助?

代码:

from Tkinter import *

class GUI():
    def __init__(self):
        root=Tk()
        frame=Frame(root,width=300,height=300)
        frame.grid(row=0,column=0)
        self.canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
        Label(self.canvas,text="1").pack()
        Label(self.canvas,text="2").pack()
        Label(self.canvas,text="3").pack()
        Label(self.canvas,text="4").pack()
        Label(self.canvas,text="5").pack()
        Label(self.canvas,text="6").pack()
        Label(self.canvas,text="1").pack()
        Label(self.canvas,text="2").pack()
        Label(self.canvas,text="3").pack()
        Label(self.canvas,text="4").pack()
        Label(self.canvas,text="5").pack()
        Label(self.canvas,text="6").pack()
        hbar=Scrollbar(frame,orient=HORIZONTAL)
        hbar.pack(side=BOTTOM,fill=X)
        hbar.config(command=self.canvas.xview)
        vbar=Scrollbar(frame,orient=VERTICAL)
        vbar.pack(side=RIGHT,fill=Y)

        self.canvas.bind('<Configure>',self._On_Configure())

        vbar.config(command=self.canvas.yview)
        self.canvas.config(width=300,height=300)
        self.canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
        self.canvas.pack(side=LEFT,expand=True,fill=BOTH)
        root.mainloop()
    def _On_Configure(self):
        self.canvas.configure(scrollregion=self.canvas.bbox('all'))

GUI = GUI()

1 个答案:

答案 0 :(得分:0)

我不会为你实现它。以下是您可能感兴趣的一些方法:

from Tkinter import *

class GUI():
    def __init__(self):
    root=Tk()
    frame=Frame(root,width=300,height=300)
    frame.grid(row=0,column=0)
    self.canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
    self.canvas_frame = Frame(self.canvas)
    self.canvas.create_window(0,0, anchor = NW, window = self.canvas_frame)
    self.labels = []
    for i in range(10):
        label = Label(self.canvas_frame,text=str(i))
        label.pack()
        self.labels.append(label)
    hbar=Scrollbar(frame,orient=HORIZONTAL)
    hbar.pack(side=BOTTOM,fill=X)
    hbar.config(command=self.canvas.xview)
    vbar=Scrollbar(frame,orient=VERTICAL)
    vbar.pack(side=RIGHT,fill=Y)

    self.canvas.bind('<Configure>',self._On_Configure) # MISTAKE! do not use () here

    vbar.config(command=self.canvas.yview)
    self.canvas.config(width=300,height=300)
    self.canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
    self.canvas.pack(side=LEFT,expand=True,fill=BOTH)
    root.mainloop()
    def _On_Configure(self, event = None):
        print self.canvas.bbox(self.canvas_frame)
        for label in self.labels:
            print label.winfo_pointerxy(), label.winfo_rootx(), label.winfo_rooty(), label.winfo_height(), label.winfo_reqheight()
        print "canvas:", self.canvas.winfo_height(), self.canvas.winfo_pointerxy()
        self.canvas.configure(scrollregion=self.canvas.bbox(self.canvas_frame))

GUI = GUI()

输出显示标签和其他像素的位置。

None
(664, 607) 0 0 21 21
(664, 607) 0 21 21 21
(664, 607) 0 42 21 21
(664, 607) 0 63 21 21
(664, 607) 0 84 21 21
(664, 607) 0 105 21 21
(664, 607) 0 126 21 21
(664, 607) 0 147 21 21
(664, 607) 0 168 21 21
(664, 607) 0 189 21 21
canvas: 304 (664, 607)

了解标签的大小或框架的大小,您可以使用vbar存储self.vbar,并在需要时执行self.vbar.pack_forget()vbar.pack()是否在函数_On_Configure中。

当你准备好滚动框架的代码时,请不要犹豫,发布它。 我记得其他人也喜欢用滚动条框架。