【问题标题】:Can't link Tkinter scrollbar to Listbox无法将 Tkinter 滚动条链接到列表框
【发布时间】:2020-08-13 02:57:39
【问题描述】:

我正在尝试使用 Tkinter 和 Sqlite3 创建零件管理器,但滚动条似乎不起作用。它显示在屏幕上,但不可用,而且似乎没有链接到列表框。
OBS:由于组织关系,我必须使用 .grid()。

from tkinter import *

root = Tk()

#Define and Grid Listbox / Scrollbar
parts_list = Listbox(root, height=12, width=86, borderwidth=3)
parts_list.grid(row=0, column=0, pady=10, padx=10)

scrollbar = Scrollbar(root)
scrollbar.grid(row=0, column=1)

#Set Scroll to Listbox
parts_list.configure(yscrollcommand=scrollbar.set)
scrollbar.configure(command=parts_list.yview)

#Populate Listbox
for x in range(20):
    parts_list.insert(END, 'Lorem Ipsum' + str(x))

root.mainloop()

【问题讨论】:

    标签: python user-interface tkinter listbox scrollbar


    【解决方案1】:

    sticky=NS 应用于滚动条小部件,试试这个:

    from tkinter import *
    
    root = Tk()
    
    #Define and Grid Listbox / Scrollbar
    parts_list = Listbox(root, height=12, width=86, borderwidth=3)
    parts_list.grid(row=0, column=0, columnspan=1)
    
    scrollbar = Scrollbar(root)
    scrollbar.grid(row=0,column=1,sticky=NS)
    
    #Set Scroll to Listbox
    scrollbar.configure(command=parts_list.yview)
    parts_list.configure(yscrollcommand=scrollbar.set)
    
    #Populate Scrollbar
    for x in range(20):
        parts_list.insert(END, 'Lorem Ipsum' + str(x))
    
    root.mainloop()
    

    【讨论】:

    • 它有效!非常感谢,泰尔!但似乎 Traversy Media 有某种神奇的编辑器,因为他不需要它来让它工作哈哈
    • @PedroSales 大声笑不知道为什么他不需要使用它。可能是不同版本的 Tkinter 或不同的操作系统。
    • 是的,我认为他在 Mac 上运行它。不过我不知道这有什么不同。
    猜你喜欢
    • 2022-10-06
    • 1970-01-01
    • 2020-02-09
    • 2020-04-28
    • 1970-01-01
    • 2017-09-23
    • 2017-12-04
    • 2014-08-30
    • 1970-01-01
    相关资源
    最近更新 更多