如何在 tkinter 中隐藏 exe 文件的命令提示符,为什么?我需要制作一个应用程序,以便轻松地将华氏度转换为摄氏度,反之亦然,我不想要臃肿的软件,这就是原因。请帮忙 代码:
`
import tkinter as tk
from tkinter import messagebox
def convert_temperature():
temp = entry.get()
try:
temp = float(temp)
if var.get() == "Celsius":
fahrenheit = temp \* (9/5) + 32
result_label.config(text=f"{temp}°C is {fahrenheit:.2f}°F")
elif var.get() == "Fahrenheit":`your text`
celsius = (temp - 32) \* (5/9)
result_label.config(text=f"{temp}°F is {celsius:.2f}°C")
except ValueError:
messagebox.showerror("Invalid input", "Please enter a valid number")
# Create the main window
root = tk.Tk()
root.title("Temperature Converter")
# Create a label and entry for temperature input
entry_label = tk.Label(root, text="Enter temperature:")
entry_label.pack()
entry = tk.Entry(root)
entry.pack()
# Create radio buttons for Celsius and Fahrenheit
var = tk.StringVar(value="Celsius")
celsius_radio = tk.Radiobutton(root, text="Celsius", variable=var, value="Celsius")
celsius_radio.pack()
fahrenheit_radio = tk.Radiobutton(root, text="Fahrenheit", variable=var, value="Fahrenheit")
fahrenheit_radio.pack()
# Create a button to trigger the conversion
convert_button = tk.Button(root, text="Convert", command=convert_temperature)
convert_button.pack()
#Create a label to display the result
result_label = tk.Label(root, text="")
result_label.pack()
#Run the application
root.mainloop()
#命令提示符打开,我希望它不打开
我猜您正在 Windows 上工作,正如您所说的
python.exe
。当您在 Windows 上安装 Python 时,安装程序会注册 2 个文件扩展名:.py
和 .pyw
.py
文件,启动 IDE(IDLE 或您可能已安装的其他 IDE).pyw
文件,启动 python 引擎来运行代码,没有 shell 窗口(Windows 上为 cmd.exe
)当您的代码使用
tkinter
创建独立窗口时,只需将文件重命名为 filename.pyw
,然后在资源管理器中双击该文件即可启动您的应用程序,而无需 shell 窗口
但这仅在用户计算机上安装了 python IDE 时才有效。因此,如果您想与计算机上没有 Python 的朋友共享代码,则必须使用
pyinstaller
等工具为您的应用程序创建一个真正的 Windows 可执行文件。正如 JRiggles 上面回忆的那样,当在 pyinstaller
文件上应用 .pyw
时,它会自动应用标志 --noconsole
,这意味着执行时“无 shell 窗口”。