Python可执行文件会弹出GUI,但按钮不起作用,CMD也会弹出

2024-10-02 20:33:42 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我创建了一个简单的程序,通过CMD关闭我的电脑。它在Thonny中都能正常工作,但在使其成为可执行文件后就不起作用了。当我打开可执行文件时,GUI会弹出,按下一个按钮后,一个新的GUI会弹出一个空的CMD

以下是完整的代码:

import tkinter as tk
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import font
from tkinter.ttk import *
import os
import time

def cancel():
    os.system("shutdown -a")
    textVar.set("Shutdown cancelled")
    
def shutdown():
    hourSel = hourCondition.get()
    if hourSel == 3600:
        textVar.set("Computer will shut down in 1 hour!")
        os.system("shutdown /s /t 3600")
    if hourSel == 7200:
        os.system("shutdown /s /t 7200")
        textVar.set("Computer will shut down in 2 hours!")
    if hourSel == 10800:
        os.system("shutdown /s /t 10800")
        textVar.set("Computer will shut down in 3 hours!")
    if hourSel == 14400:
        os.system("shutdown /s /t 14400")
        textVar.set("Computer will shut down in 4 hours!")
        textVar.set("Computer will shut down in 4 hours!")
    if hourSel == 0:
  
        try:
            
            val = int(value.get())
            seconds2Minutes = round(val * 0.016666667,2)
            if val > 0:
                textVar.set("Computer will shut down in " + str(seconds2Minutes) + " min")
                os.system("shutdown /s /t " + str(val))
            if val < 0:
                textVar.set("Choose correct value!")
        except ValueError:
            textVar.set("Choose correct value!")
    

root = tk.Tk()
root.title("Shutdown")
root.geometry("400x500")
root.configure(background = "#082838")


myFont = font.Font(size=16)
myFont2 = font.Font(size = 10)
radioStyle = ttk.Style()
radioStyle.configure("radioStyle.TRadiobutton", background = "#082838", foreground = "white")

textVar = StringVar()
textVar.set("Choose when to shutdown your computer(sec)")
status=Label(text="Choose when to shut down your computer!",background = "#082838", foreground = "white", textvariable = textVar).place(anchor = "s", x = 200, y = 385)

valueLabel = ttk.Label(root, text="Value(sec): ", background = "#082838",foreground="white")
value = ttk.Entry(root)
value.place(anchor = "s", x = 200, y = 100)
valueLabel.place(anchor ="se", x = 100, y = 100)

button1 = tkinter.Button(root, text = "SHUTDOWN", bg = "lightgrey", command = shutdown)
button1.place(anchor = "s",x = 200, y = 200)
button1['font'] = myFont

button2 = tkinter.Button(root, text = "Cancel shutdown", bg = "lightgrey", command = cancel)
button2.place(anchor = "n", x = 200, y = 400)
button2["font"] = myFont2

#Radiobuttons
hourCondition = IntVar()
hour1 = ttk.Radiobutton(root, text= "1 hour (3600s)", style = "radioStyle.TRadiobutton", variable = hourCondition, value = 3600)
hour2 = ttk.Radiobutton(root, text = "2 hours (7200s)", style = "radioStyle.TRadiobutton", variable = hourCondition, value = 7200)
hour3 = ttk.Radiobutton(root, text = "3 hours(10800s)", style = "radioStyle.TRadiobutton", variable = hourCondition, value = 10800)
hour4 = ttk.Radiobutton(root, text = "4 hours (14400s)",  style = "radioStyle.TRadiobutton",variable = hourCondition, value = 14400)
hour5 = ttk.Radiobutton(root, text = "Custom", style = "radioStyle.TRadiobutton", variable = hourCondition, value = 0)

hour1.place(anchor = "s", x = 200, y = 230)
hour2.place(anchor = "s", x = 200, y = 260)
hour3.place(anchor = "s", x = 200, y = 290)
hour4.place(anchor = "s", x = 200, y = 320)
hour5.place(anchor = "s", x = 200, y = 350)



tk.mainloop()

图片说明: 下面的关机窗口是最初打开的窗口(可执行),当单击“关机”或“取消关机”时,新的关机(重复)窗口将打开,并显示空CMD。它应该在选定的时间关闭计算机。下面的窗口显示“无响应”,你知道我做错了什么吗?我使用pyinstaller--onefile-w命令生成一个可执行文件。 Text


Tags: textimportifvalueostkinterplaceroot