简体   繁体   English

Python 3、Tkinter:退出window时如何退出代码?

[英]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.

相关问题 如何在 Tkinter、Python 中退出命令? - How do I exit command in Tkinter, Python? 当用户单击 tkinter 根 window 关闭框时,如何从 asyncio 事件循环 run_forever() 干净地退出我的 python3 应用程序? - How do I exit my python3 application cleanly from asyncio event loop run_forever() when user clicks tkinter root window close box? 更改图像后如何退出tkinter窗口? - how can I exit a tkinter window after changing images? 如何退出 python 中的循环 - How do I exit a loop in python 在 Tkinter 窗口 Mac OSX 中单击退出时 Python 3.6 崩溃 - Python 3.6 crashes when clicking exit in Tkinter window Mac OSX 从 Windows 批处理文件运行时,如何从 exe 中获取 Python sys.exit() 代码? - How do I get the Python sys.exit() code from an exe when running it from a Windows batch file? 如何通过 function 退出整个程序? - How do I exit out of an entire program through a function? Python - 1秒后我无法退出GUI,当调用gtk.main()时它永远不会退出,我该如何退出? - Python - After 1 second i cant exit the GUI, when ever the gtk.main() is called it never exit, how can i exit? Python Tkinter-使用退出按钮关闭子窗口 - Python Tkinter - closing a child window with an exit button 无法在tkinter窗口python上退出全屏 - Cant exit fullscreen on tkinter window python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM