选择 Tkinter 列表框中的所有项目后如何显示消息框?

问题描述 投票:0回答:1

我编写了一个 GUI 调用脚本,其中每一行都是列表框中的一个项目。我想要一个消息框来告诉用户列表框中的所有项目都已完成。如果可能的话请帮忙。

if int(listbox.size()) == len(listbox.curselection()):
    messagebox.showinfo("Well done!", "You've completed the call script!")

我需要在选择列表框中的所有项目后显示消息框 但目前它什么也没做,甚至没有错误消息

python tkinter listbox
1个回答
0
投票

只要选择发生变化,只要调用它,您的代码就应该可以工作。

下面是一个简单的例子:

import tkinter as tk
from tkinter import messagebox

def on_select(event):
    listbox = event.widget
    if int(listbox.size()) == len(listbox.curselection()):
        messagebox.showinfo("Well done!", "You've completed the call script!")

root = tk.Tk()

listbox = tk.Listbox(root, selectmode="multiple", activestyle="none")
listbox.pack()
# call on_select() whenever selection is changed
listbox.bind("<<ListboxSelect>>", on_select)

# insert dummy items
for i in range(5):
    listbox.insert("end", i+1)

root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.