到目前为止,我已经创建了一个带有一些功能按钮/菜单的框架。我的菜单按钮之一打开了一个新的“顶层”窗口。这个新的顶级窗口基本上是一个论坛填写页面。页面末尾是完成按钮。完成按钮关闭了tkiniter顶级窗口,同时离开了根窗口。我希望在按下按钮后执行代码行。
我的第一个想法是设置完成按钮命令以使窗口“撤回”。如果执行此操作,它将撤回窗口,但是如果我尝试运行if检查,则在按下按钮之前已经检查了该检查。对此,最简单的解决方案是创建一个函数,该函数撤回窗口并执行我想要的其他操作,但是,当我尝试将函数作为顶级完成按钮的命令传递时,它不会调用该函数。
任何有关如何使按钮关闭窗口然后执行附加代码(仅在按下按钮之后)的建议都将不胜感激。我已在下面附加了有问题的代码部分。预先感谢您的帮助。(旁注Computer类在另一个文件中处理)。(我正在使用Python 3.x)
def draw_new_computer_entry(self):
t = Toplevel(self)
t.resizable(0,0)
t.wm_title("Solution Center: Add New Computer")
t.geometry("300x300")
nameLabel = Label(t, text="Computer Name:")
nameLabel.place(x=0,y=10)
a = computer()
comp_nameEntry = Entry(t, width=30)
comp_nameEntry.place(master=None, x=100, y=10)
a.name = comp_nameEntry.get()
comp_makerEntry= Entry(t,width=30)
comp_makerEntry.place(x=100, y=50)
a.maker = comp_makerEntry.get()
makerLabel= Label(t, text="Maker:")
makerLabel.place(x=55,y=50)
graphics_cardEntry= Entry(t, width=30)
graphics_cardEntry.place(x=100,y=90)
a.gpu = graphics_cardEntry.get()
graphics_cardLabel= Label(t, text="Graphics Card:")
graphics_cardLabel.place(x=15, y=90)
processorEntry= Entry(t, width=30)
processorEntry.place(x=100, y=130)
a.processor = processorEntry.get()
processorLabel= Label(t, text="Processor:")
processorLabel.place(x=38, y=130)
hard_driveEntry= Entry(t, width=30)
hard_driveEntry.place(x=100, y=170)
a.hard_drive = hard_driveEntry.get()
hard_driveLabel= Label(t, text="Hard Drive:")
hard_driveLabel.place(x=30, y=170)
ramEntry= Entry(t, width=30)
ramEntry.place(x=100, y=210)
ramLabel= Label(t,text="Ram:")
ramLabel.place(x=65,y=210)
doneButton = Button(t, text="done", command=t.withdraw)
doneButton.place(x=265, y=275)
您所要做的就是创建一个函数来关闭窗口,然后执行其他代码。
这是您的代码段:
def donePressed():
t.withdraw() # Or t.destroy(), depending if you need to open this window again
# Enter Code Here
print('code after window close')
doneButton = Button(t, text="done", command=donePressed)
doneButton.place(x=265, y=275)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句