如何解决关闭上一个顶级窗口后,创建顶级tkinter窗口
我有n个元素的列表,我需要为每个元素创建一个弹出菜单。每个弹出窗口都会包含一些复选框。
条件:新的“顶级”弹出窗口必须在关闭前一个“顶级”窗口后打开,并且不能同时全部打开
我的代码:
from tkinter import *
#root gui
root = Tk()
root.title("test")
root.geometry("300x400")
# Need three popup windows
a = ["one","two","three"]
b = []
def open():
for _ in range(len(a)):
top = Toplevel()
top.title("selections")
def next_window():
top.destroy()
show() # This function is supposed to show the selections of each popup window on root gui
for i in range(3):
b.append(IntVar())
b[i].set(0) # de selecting all checkboxes intiially
# checkboxes
Checkbutton(top,text=a[i],variable=b[i]).pack()
Button(top,text = "Submit",command=next_window).pack()
Button(top,text = "skip",command=top.destroy).pack() # this button is used to skip the popup if no selection required
def show():
# printing selections made on each popup window
for i in range(3):
Label(root,text=b[i].get()).pack()
mB = Button(root,text="print selections",command=open).pack()
root.mainloop()
解决方法
您需要在for循环的末尾调用top.wait_window()
:
for _ in range(len(a)):
top = Toplevel(root)
top.title("selections")
def next_window():
top.destroy()
show() # This function is supposed to show the selections of each popup window on root gui
for i in range(3):
b.append(IntVar())
b[i].set(0) # de selecting all checkboxes intiially
# checkboxes
Checkbutton(top,text=a[i],variable=b[i]).pack()
Button(top,text = "Submit",command=next_window).pack()
Button(top,text = "skip",command=top.destroy).pack() # this button is used to skip the popup if no selection required
top.grab_set() # route all events to this window
top.wait_window() # wait for current window to close
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。