点击按钮后如何延迟关闭tkinter窗口?
我有一个简单的tkinter gui,它显示一个单选按钮和ok cancel按钮。当点击ok按钮时,我想继续运行我的程序,5秒钟后关闭窗口点击按钮后如何延迟关闭tkinter窗口?,tkinter,delay,destroy,Tkinter,Delay,Destroy,我有一个简单的tkinter gui,它显示一个单选按钮和ok cancel按钮。当点击ok按钮时,我想继续运行我的程序,5秒钟后关闭窗口 import tkinter as tk from tkinter.ttk import * gui = tk.Tk() gui.geometry('330x150') # set up radio buttons selected = tk.IntVar() # selected holds radio button currently select
import tkinter as tk
from tkinter.ttk import *
gui = tk.Tk()
gui.geometry('330x150')
# set up radio buttons
selected = tk.IntVar() # selected holds radio button currently selected
def ok_clicked():
gui.after(5000, lambda : gui.destroy())
#run stuff while waiting for the gui to close
if selected.get() == 0:
# run HS
import Open_HS
else:
# run KA
import Open_KA
def cancel_clicked():
gui.destroy()
hs_btn = Radiobutton(gui, width=15, text="Radio 1", value=0,variable=selected).place(x=50, y=40)
ok_btn = Button(gui, width=9, text="OK", command=ok_clicked).place(x=180, y=115)
cancel_btn = Button(gui, width=9, text="Cancel", command=cancel_clicked).place(x=250, y=115)
gui.mainloop()
这显然不行,因为我在打电话
gui.mainloop.
在我跟你安排延迟之前
gui.after(5000, lambda : gui.destroy())
但如何修复呢
感谢使用
gui进行。使用gui后,您可以实现所需:
import tkinter as tk
def ok_clicked():
gui.after(5000, gui.destroy)
def cancel_clicked():
gui.destroy()
if __name__ == '__main__':
gui = tk.Tk()
gui.geometry('330x150')
selected = tk.IntVar()
tk.Radiobutton(gui, width=15, text="Radio 1", value=0, variable=selected).place(x=50, y=40)
tk.Button(gui, width=9, text="OK", command=ok_clicked).place(x=180, y=115)
tk.Button(gui, width=9, text="Cancel", command=cancel_clicked).place(x=250, y=115)
gui.mainloop()
你为什么说“这显然不起作用”?它对我有用。当我点击按钮时,窗口在5秒后被破坏。嗯,当我现在尝试时也是如此。但在我的实际代码中,它没有包含导入指令,可以在.after命令的正下方导入一个单独的文件。我已经编辑了代码来说明我的意思。很抱歉,我不认为这是问题所在,所以之前将其忽略。由于您正在导入自定义模块,因此您的代码不会按发布的方式运行。您需要包括导致问题的Open\HS
的最小可能实现。好的,谢谢。我现在明白了。我原以为问题出在我发布的代码片段中,但现在我看到它出现在我的Open_HS模块中。我需要一段时间来解决它。再次感谢。谢谢你的洞察力。我查看了后面的代码,并将gui.destroy()行放在锁定执行的部分(SeleniumWebDriver)之后,现在可以正常工作了。