【问题标题】:Python listbox expand and hide after clicks单击后Python列表框展开和隐藏
【发布时间】:2017-07-17 11:08:56
【问题描述】:

我在下面有以下示例代码。有没有办法展开列表框,点击时显示 100 个数字中的 10 个? And when selecting one of the numbers, does the listbox hide the others again?

from tkinter import *

root = Tk()

scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)

listbox = Listbox(root)
listbox.pack()

for i in range(100):
    listbox.insert(END, i)

# attach listbox to scrollbar
listbox.config(yscrollcommand=scrollbar.set, height = 1)
scrollbar.config(command=listbox.yview)

mainloop()

【问题讨论】:

    标签: python tkinter listbox


    【解决方案1】:

    您可以将事件处理程序连接到<Button-1> 信号,并每次切换列表框高度:

    def listbox_clicked(event):
        listbox = event.widget
        if listbox['height'] == 1:
            listbox.config(height=10)
        else:
            listbox.config(height=1)
    
    listbox.bind('<Button-1>', listbox_clicked)
    

    【讨论】:

    • 这是一个比我的帖子更好的答案,但您需要同时包含两者才能使其正常工作。
    【解决方案2】:

    我相信打包选项会有所帮助。

    scrollbar.pack(side=RIGHT, fill=Y, expand=True)
    listbox.pack(fill=Y, expand=True)
    

    【讨论】:

      【解决方案3】:

      非常感谢您的帮助,但我只需要一个组合框!

      【讨论】:

        猜你喜欢
        • 2020-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多