使用繁體中文,在linux mint21.3下,使用python語言,使用UI界面,要設計一個按鈕,功能是按下後,讓本機電腦重新啟動,要如何處理,請給完整範例,謝謝
sudo apt update
sudo apt install python3-tk
import tkinter as tk
import os
import sys
def restart_system():
"""重新啟動系統的函數"""
# 在 Linux 系統中使用 'reboot' 命令
try:
os.system("sudo reboot")
except Exception as e:
print(f"重新啟動時發生錯誤: {e}")
def create_gui():
"""創建 GUI 界面"""
root = tk.Tk()
root.title("重新啟動電腦")
# 設定按鈕
restart_button = tk.Button(root, text="重新啟動電腦", command=restart_system)
restart_button.pack(pady=20)
# 設定視窗大小
root.geometry("300x100")
root.mainloop()
if __name__ == "__main__":
# 確保腳本以 sudo 身份運行
if not os.geteuid() == 0:
print("請以 sudo 權限執行此腳本")
sys.exit(1)
create_gui()
chmod +x restart_gui.py
sudo python3 restart_gui.py
加上關機
import tkinter as tk
import os
import sys
def restart_system():
"""重新啟動系統的函數"""
try:
os.system("sudo reboot")
except Exception as e:
print(f"重新啟動時發生錯誤: {e}")
def shutdown_system():
"""關閉系統的函數"""
try:
os.system("sudo shutdown now")
except Exception as e:
print(f"關機時發生錯誤: {e}")
def create_gui():
"""創建 GUI 界面"""
root = tk.Tk()
root.title("系統管理")
# 設定視窗大小
root.geometry("300x200")
# 設定字體顏色和大小
button_font = ('Arial', 14)
# 重新啟動按鈕
restart_button = tk.Button(root, text="重新啟動電腦", command=restart_system, font=button_font, fg='blue')
restart_button.pack(pady=20)
# 關機按鈕
shutdown_button = tk.Button(root, text="關閉電腦", command=shutdown_system, font=button_font, fg='blue')
shutdown_button.pack(pady=10)
root.mainloop()
if __name__ == "__main__":
# 確保腳本以 sudo 身份運行
if not os.geteuid() == 0:
print("請以 sudo 權限執行此腳本")
sys.exit(1)
create_gui()
sudo apt install python3-pip
pip install pyinstaller
pyinstaller --onefile your_script.py
sudo apt install build-essential libssl-dev libffi-dev python3-dev
# 安裝虛擬環境模組
sudo apt install python3-venv
# 創建一個虛擬環境
python3 -m venv myenv
# 啟用虛擬環境
source myenv/bin/activate
# 在虛擬環境中安裝 PyInstaller
pip install pyinstaller
pyinstaller --onefile your_script.py
接上,使用UI界面,PyInstaller打包工具,要如何設計,請給可以實際使用的示例
要設計一個使用 GUI 界面的 PyInstaller 打包工具,你可以使用 Tkinter 來創建簡單的 GUI。以下是實際使用的示例,這個示例提供了一個界面,讓用戶可以選擇要打包的 Python 檔案,並選擇是否使用 --onefile 參數。
import tkinter as tk
from tkinter import filedialog, messagebox
import subprocess
import os
class PyInstallerGUI:
def __init__(self, master):
self.master = master
master.title("PyInstaller 打包工具")
self.label = tk.Label(master, text="選擇要打包的 Python 檔案:")
self.label.pack()
self.file_path = tk.StringVar()
self.file_entry = tk.Entry(master, textvariable=self.file_path, width=50)
self.file_entry.pack()
self.browse_button = tk.Button(master, text="瀏覽", command=self.browse_file)
self.browse_button.pack()
self.onefile_var = tk.BooleanVar()
self.onefile_check = tk.Checkbutton(master, text="打包為單一檔案 (--onefile)", variable=self.onefile_var)
self.onefile_check.pack()
self.build_button = tk.Button(master, text="打包", command=self.build)
self.build_button.pack()
def browse_file(self):
file_path = filedialog.askopenfilename(filetypes=[("Python files", "*.py")])
if file_path:
self.file_path.set(file_path)
def build(self):
python_file = self.file_path.get()
if not python_file:
messagebox.showerror("錯誤", "請選擇一個 Python 檔案。")
return
command = ["pyinstaller"]
if self.onefile_var.get():
command.append("--onefile")
command.append(python_file)
try:
subprocess.run(command, check=True)
messagebox.showinfo("成功", "打包完成!")
except subprocess.CalledProcessError as e:
messagebox.showerror("錯誤", f"打包失敗:{e}")
if __name__ == "__main__":
root = tk.Tk()
app = PyInstallerGUI(root)
root.mainloop()