python GUI tkinter滚动条不适用于画布

时间:2017-07-29 15:31:56

标签: python user-interface tkinter tkinter-canvas

我正在使用Tkinter学习python GUI。我刚刚创建了两个列表框并在画布中填充了这些列表框,以便我可以使用滚动条配置画布,当我滚动画布时,这两个列表框滚动togather。但有些东西不起作用。

这是结构:

canvas = Canvas(master)
scrollBar = Scrollbar(master)
movieListBox = Listbox(canvas) 
statusListBox = Listbox(canvas)
canvas.config(yscrollcommand = scrollBar.set)
movieListBox.config(width = 55)
statusListBox.config(width = 8)
movieListBox.pack(fill = "y", side = "left")
statusListBox.pack(fill = "y", side = "right")
canvas.pack(side = "left", fill = "y", expand = "true")
scrollBar.config(command = canvas.yview)
scrollBar.pack(fill = "y", side = "left")

for i in range(500):
    movieListBox.insert(i, "movie name")
    statusListBox.insert(i, "downloading")

master.mainloop()

enter image description here

1 个答案:

答案 0 :(得分:0)

我本人对tkinter和python还是很陌生,但是我确实找到了可行的方法。这是来自另一个问题(我没有编写代码),该问题涉及将两个不同长度的列表框链接到同一滚动条。如果您复制他们的代码(如下),您会发现它对于相同长度的列表框工作正常。您的问题已经很老了,但希望对您有所帮助。干杯。

我在哪里找到这个

Scrolling two listboxes of different lengths with one scrollbar

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

class App(object):
    def __init__(self,master):
        scrollbar = tk.Scrollbar(master, orient='vertical')
        self.lb1 = tk.Listbox(master, yscrollcommand=scrollbar.set)
        self.lb2 = tk.Listbox(master, yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.yview)
        scrollbar.pack(side='right', fill='y')
        self.lb1.pack(side='left', fill='both', expand=True)
        self.lb2.pack(side='left', fill='both', expand=True)

    def yview(self, *args):
        """connect the yview action together"""
        self.lb1.yview(*args)
        self.lb2.yview(*args)


root = tk.Tk()
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("320x180+130+180")
root.title("connect 2 listboxes to one scrollbar")

app = App(root)

# load the list boxes for the test
for n in range(64+26, 64, -1): #listbox 1
    app.lb1.insert(0, chr(n)+'ell')

for n in range(70+30, 64, -1):    
    app.lb2.insert(0, chr(n)+'ell') #listbox 2

root.mainloop()