[英]Python 3, Tkinter: How do I exit the code when I exit out of the window?
root = Tk()
icon2 = PhotoImage(file="Arduino UI Icon 2.png")
root.iconphoto(True, icon2)
def LightLED():
global root
root.withdraw()
ledWin = Tk()
ledWin.resizable(False, False)
ledWin.title("Arduino UI")
ledWin.geometry('1280x720')
pinMess = Message(ledWin, text="Enter Pin Number: ", width=200, font=("Arial", 10))
pinMess.place(x=0, y=10)
pinEntry = Entry(ledWin, width=3)
pinEntry.place(x=40, y=35)
def SetPin():
global led, pin
try:
pin = pinEntry.get()
led = board.get_pin("d:" + pin + ":p")
except:
pass
pinBut = Button(ledWin, text="Set Pin", width=10, height=1, font=("Arial", 10),
command=SetPin)
pinBut.place(x=70, y=32)
def LEDbOn():
led.write(1)
def LEDbOff():
led.write(0)
def LEDQuit():
global root
led.write(0)
ledWin.destroy()
root.deiconify()
ledbOn = Button(ledWin, text="Start", command=LEDbOn, width=8, height=1)
ledbOn.place(x=170, y=30)
ledbOff = Button(ledWin, text="Stop", command=LEDbOff)
ledbOff.place(x=185, y=70)
ledwinQuit = Button(ledWin, text="Exit", command=LEDQuit)
ledwinQuit.place(x=187, y=110)
ledWin.mainloop()
def MainMenu():
root.resizable(False, False)
root.title("Arduino UI")
root.geometry('1280x720')
menubar = Menu(root)
root.config(menu=menubar)
fileMenu = Menu(menubar, tearoff=0)
subMenu = Menu(fileMenu, tearoff=0)
menubar.add_cascade(label="Programs", menu=fileMenu)
fileMenu.add_command(label="Light LED", command=LightLED)
root.mainloop()
if __name__ == "__main__":
MainMenu()
exit()
So, I want to exit out of the entire program if IX out of the window (Without pressing the Exit button).因此,如果 IX 退出 window(不按退出按钮),我想退出整个程序。 But, when I do that, because the root is still active and only hidden, the code will not exit out.
但是,当我这样做时,因为根仍然处于活动状态并且只是隐藏,所以代码不会退出。 Then it will keep running as like a background task or something.
然后它将像后台任务或其他东西一样继续运行。 So, how do I make it exit when I hit the X?
那么,当我击中 X 时如何让它退出呢? I have already tried to put the
exit()
after the ledWin.mainloop()
, but that doesn't work, and I have no idea why.我已经尝试将
exit()
放在ledWin.mainloop()
之后,但这不起作用,我不知道为什么。 It also happens when I put the exit()
after the LightLED function and before the MainMenu function, which makes sense because everything is happening within the MainMenu function.当我将
exit()
放在 LightLED function 之后和 MainMenu function 之前时,也会发生这种情况,这是有道理的,因为一切都在 MainMenu ZC1C425268E68385D14AB5074C17ZA 中发生。 But, is there any place to put it that will close the entire program?但是,有什么地方可以关闭整个程序吗?
I am not 100% if this is what you meant you are trying to do.如果这是您的意思,我不是 100%。
# import required module
from tkinter import *
# create object
root = Tk()
# create button to implement destroy()
Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.