簡體   English   中英

python GUI tkinter滾動條不適用於畫布

[英]python GUI tkinter scrollbar not working for canvas

我正在用Tkinter學習python GUI。 我剛剛創建了兩個列表框,並在畫布中填充了它們,以便可以使用滾動條配置畫布,並且當我滾動畫布時,這兩個列表框一起滾動。 但有些東西不起作用。

結構如下:

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()

在此處輸入圖片說明

我本人對tkinter和python還是很陌生,但是我確實找到了可行的方法。 這是來自另一個問題(我沒有編寫代碼),該問題涉及將兩個不同長度的列表框鏈接到同一滾動條。 如果您復制他們的代碼(如下),您會發現它對於相同長度的列表框工作正常。 您的問題已經很老了,但希望對您有所幫助。 干杯。

我在哪里找到這個:

使用一個滾動條滾動兩個不同長度的列表框

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM