【问题标题】:Python tk scrollbar on listbox not working in windows 10列表框上的 Python tk 滚动条在 Windows 10 中不起作用
【发布时间】:2022-01-26 15:24: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()

我是不是做错了什么?

非常感谢。

【问题讨论】:

    标签: python tkinter windows-10 listbox scrollbar


    【解决方案1】:

    我不知道你是如何使用tkinter 在 Linux 中工作的,但无论如何你将滚动条设置为错误的轴。

    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 列。

    【讨论】:

      猜你喜欢
      • 2010-11-19
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多