我有一个使用Tkinter
的程序,我正在尝试将命令分配到我的root
窗口中的按钮,该按钮会打开一个附加窗口。我正在使用Toplevel()
,但每当我点击按钮时,我都会分配命令,两个窗口打开,一个带有我root
窗口的名称,另一个带有名称我已分配的附加窗口。
我尝试使用.withdraw
和.destroy
来隐藏或删除这个额外的root
窗口,但似乎没有任何效果。
这是我的代码:
import Tkinter
from Tkinter import *
root = Tk()
root.wm_title("VACS")
# # Top label # #
SetParameters = Label(text="Set Parameters", width=110, relief=RIDGE)
SetParameters.grid(row=1, column=0, columnspan=7, padx=5, pady=5)
# # Spatial freq settings # #
SpatialFreq = Label(text="Spatial Frequency", width=15, relief=RIDGE)
SpatialFreq.grid(row=3, column=0, padx=5, pady=5)
From1 = Label(text="from")
From1.grid(row=3, column=1, padx=5, pady=5)
Select1 = Spinbox(from_=0, to=10, width=25)
Select1.grid(row=3, column=2, padx=5, pady=5)
To1 = Label(text="to")
To1.grid(row=3, column=3, padx=5, pady=5)
Select2 = Spinbox(from_=0, to=10, width=25)
Select2.grid(row=3, column=4, padx=5, pady=5)
Steps = Label(text="in steps of")
Steps.grid(row=3, column=5, padx=5, pady=5)
Select3 = Spinbox(from_=0, to=10, width=25)
Select3.grid(row=3, column=6, padx=5, pady=5)
# # Contrast settings # #
Contrast = Label(text="Contrast", width=15, relief=RIDGE)
Contrast.grid(row=5, column=0, padx=5, pady=5)
From2 = Label(text="from")
From2.grid(row=5, column=1, padx=5, pady=5)
Select4 = Spinbox(from_=0, to=10, width=25)
Select4.grid(row=5, column=2, padx=5, pady=5)
To2 = Label(text="to")
To2.grid(row=5, column=3, padx=5, pady=5)
Select5 = Spinbox(from_=0, to=10, width=25)
Select5.grid(row=5, column=4, padx=5, pady=5)
Steps2 = Label(text="in steps of")
Steps2.grid(row=5, column=5, padx=5, pady=5)
Select6 = Spinbox(from_=0, to=10, width=25)
Select6.grid(row=5, column=6, padx=5, pady=5)
# # Test button # #
Test = Button(text="Begin Test", width=25, command=Top)
Test.grid(row=6, column=0, columnspan=7, pady=5)
# # Directory input window # #
def Top():
Toplevel()
Toplevel().wm_title("Directory")
root.mainloop()
如果在root
窗口中单击“开始测试”,则会弹出两个附加内容。我只想要一个说“目录”的那个。
有什么想法吗?
答案 0 :(得分:2)
您正在创建两个,因为Toplevel()
是构造函数调用:
Toplevel()
Toplevel().wm_title("Directory")
相反,创建一个并保存它:
top = Toplevel()
top.wm_title("Directory")