Python 列表框上的 tk 滚动条在 windows 10 中不起作用
Python tk scrollbar on listbox not working in windows 10
我有 python3 tk 代码似乎在 linux (ubuntu) 中工作正常,但奇怪的是在 windows 中不起作用 10. 列表框上的滚动条不滚动 windows ...
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
然后适当组合fill
和 side
,有时也是 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
混淆。理想情况下,在这种情况下,sb1
和 sb2
应该分别是第 1 列和第 0 列。
我有 python3 tk 代码似乎在 linux (ubuntu) 中工作正常,但奇怪的是在 windows 中不起作用 10. 列表框上的滚动条不滚动 windows ...
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
然后适当组合fill
和 side
,有时也是 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
混淆。理想情况下,在这种情况下,sb1
和 sb2
应该分别是第 1 列和第 0 列。