簡體   English   中英

Python tk 列表框上的滾動條在 windows 10 中不起作用

[英]Python tk scrollbar on listbox not working in windows 10

我的 python3 tk 代碼在 linux (ubuntu) 中似乎可以正常工作,但奇怪的是在 windows 10 中不起作用。列表框上的滾動條在 Z0F4137ED1502B5045D608B 中不滾動...

import tkinter as tk

root = tk.Tk()
root.title("My MHT")
root.geometry("500x500")

# create all of the main containers
toppest_frame = tk.Frame(root, bg='thistle3', width=500, height=25, pady=3)
top_frame = tk.Frame(root, bg='cyan', width=500, height=250, pady=3)
bottom_frame = tk.Frame(root, bg='lavender', width=500, height=250, pady=3)

# layout all of the main containers
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=1)
root.grid_rowconfigure(2, weight=1)
root.grid_columnconfigure(0, weight=1)

toppest_frame.grid(row=0, sticky="nesw")
top_frame.grid(row=1, sticky="nesw")
bottom_frame.grid(row=2, sticky="nesw")

#create scrollbar
sb1 = tk.Scrollbar(top_frame,orient="vertical")
sb1.grid(row=0,column=5)

sb2 = tk.Scrollbar(top_frame,orient="horizontal")
sb2.grid(row=1,columnspan=5)

# create the widgets for the top frame
listBox = tk.Listbox(top_frame, relief="sunken",bd=2,selectmode="single")

# layout the widgets in the top frame
top_frame.grid_rowconfigure(0,weight=1)
top_frame.grid_columnconfigure(0,weight=1)
listBox.grid(row=0,sticky="nesw",columnspan=5)

#populate listbox
listBox.delete(0, "end")
for values in range(100):
    listBox.insert("end", values)

#attach scrollbar to listbox
listBox.configure(xscrollcommand=sb1.set)
sb1.configure(command=listBox.yview)

listBox.configure(yscrollcommand=sb2.set)
sb2.configure(command=listBox.xview)

root.mainloop()

難道我做錯了什么?

非常感謝。

我不知道你是如何讓它在 Linux 和tkinter中工作的,但無論如何你正在為錯誤的軸設置滾動條。

listBox.configure(yscrollcommand=sb1.set) # Was xscrollcommand
sb1.configure(command=listBox.yview)

listBox.configure(xscrollcommand=sb2.set) # Was yscrollcommand
sb2.configure(command=listBox.xview)

然后,您還需要滾動條沿其字段擴展,使用grid您使用sticky ,如果您使用pack則適當組合fillside ,有時也會expand

sb1 = tk.Scrollbar(top_frame,orient="vertical")
sb1.grid(row=0,column=5,sticky='ns') # Expand north to south

sb2 = tk.Scrollbar(top_frame,orient="horizontal")
sb2.grid(row=1,columnspan=5,sticky='ew') # Expand east to west

我也希望您有充分的理由使用columnspan=5並且不要將其與column混淆。 在這種情況下,理想情況下, sb1sb2應該分別是第 1 列和第 0 列。

暫無
暫無

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

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