【问题标题】:Create Toplevel tkinter Window after the closure of previous Toplevel window关闭前一个顶层窗口后创建顶层 tkinter 窗口
【发布时间】:2020-08-14 11:06:56
【问题描述】:

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

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

我的代码:

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()

我的担忧:我现在正在同时打开所有三个弹出窗口。

【问题讨论】:

    标签: python user-interface tkinter toplevel


    【解决方案1】:

    你需要在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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      相关资源
      最近更新 更多