首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在以前的Toplevel窗口关闭后创建Toplevel tkinter窗口

在以前的Toplevel窗口关闭后创建Toplevel tkinter窗口
EN

Stack Overflow用户
提问于 2020-08-14 11:06:56
回答 1查看 127关注 0票数 0

我有一个n个元素的列表,我需要为每个元素创建一个弹出菜单。每个弹出窗口都包含一些复选框。

条件:新的Toplevel弹出窗口必须在关闭以前的Toplevel窗口后打开,而不是同时打开所有窗口

我的代码:

代码语言:javascript
复制
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()

,My concern,:这三个弹出窗口现在都同时为我打开。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-14 11:23:09

您需要在for循环的末尾调用top.wait_window()

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63411575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档