from tkinter import*
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import time
import matplotlib.pyplot as plt
import re
import tkinter.font as font
root=Tk()
root.geometry("700x900+500+250")
my_notebook = ttk.Notebook(root)
my_notebook.pack()
my_frame1=Frame(my_notebook,width=500,height=500)
my_frame1.pack(fill="both",expand=1)
my_notebook.add(my_frame1,text="my menu")
def open_popup(choice):
choice == value_inside.get()
if choice==options_list[0]:
top= Toplevel(root)
top.geometry("750x250")
top.title(choice)
Label(top, text= "1", font=('Mistral 18 bold')).place(x=150,y=80)
elif choice==options_list[1]:
top= Toplevel(root)
top.geometry("750x250")
top.title(choice)
Label(top, text= "2", font=('Mistral 18 bold')).place(x=150,y=80)
elif choice==options_list[2]:
top= Toplevel(root)
top.geometry("750x250")
top.title(choice)
Label(top, text= "3", font=('Mistral 18 bold')).place(x=150,y=80)
elif choice==options_list[3]:
top= Toplevel(root)
top.geometry("750x250")
top.title(choice)
Label(top, text= "4", font=('Mistral 18 bold')).place(x=150,y=80)
elif choice==options_list[4]:
top= Toplevel(root)
top.geometry("750x250")
top.title(choice)
Label(top, text= "5", font=('Mistral 18 bold')).place(x=150,y=80)
elif choice==options_list[5]:
top= Toplevel(root)
top.geometry("750x250")
top.title(choice)
Label(top, text= "6", font=('Mistral 18 bold')).place(x=150,y=80)
elif choice==options_list[6]:
top= Toplevel(root)
top.geometry("750x250")
top.title(choice)
Label(top, text= "7", font=('Mistral 18 bold')).place(x=150,y=80)
options_list = ["Bz Vs R", "Bz Vs Z", "Br Vs R", "Br Vs Z","|B| Vs R", "|B| Vs Z","B Contour Map"]
value_inside = StringVar()
value_inside.set("B-field Types")
B_field_option=OptionMenu(my_frame1,value_inside,*options_list,command=open_popup)
B_field_option.place(x=110,y=190)
root.mainloop()
大家好,
我们一直在尝试使用Tkinter构建GUI。一切都很好,除了当我们试图打开一个新的顶层时,前一个也保持打开状态。当我们选择一个新选项时,我们希望关闭上一个顶层(即上一个选项(。换言之,我们希望在不开放新平台的情况下更新顶层正在发生的事情。
如果你能帮助我们,我们将非常高兴。
提前感谢:(
如果一次只允许一个选项,并且value_inside.get()
中的值始终用作窗口分区,我不确定为什么需要打开多个窗口。我想,只要选择,你就可以简单地实例化一个Toplevel
窗口
top = None
def open_popup(top): # pass in 'top'
choice = value_inside.get()
if top == None or not Toplevel.winfo_exists(top):
top = Toplevel(root)
top.geometry("750x250")
top.title(choice )
else:
top.configure(title=choice) # update the existing window
Label(top, text= "1", font=('Mistral 18 bold')).place(x=150,y=80)
然后稍微修改B_field_option
,将top
作为参数
B_field_option=OptionMenu(
my_frame1,
value_inside,
*options_list,
command=lambda t=top: open_popup(t)
)