在Ubuntu上開發Python圖形界面(GUI)有多種方法,以下是一些常用的庫和步驟:
EasyGUI是一個簡單易用的Python庫,適合快速創建基本的圖形界面。
安裝EasyGUI:
pip install easygui
示例代碼:
import easygui
easygui.msgbox("Hello, EasyGUI!", title="Greeting")
name = easygui.enterbox("What is your name?", title="Name Input")
easygui.msgbox(f"Hello, {name}!", title="Greeting")
choices = ["Apple", "Banana", "Cherry"]
choice = easygui.choicebox("Which fruit do you like best?", choices=choices)
easygui.msgbox(f"You selected: {choice}", title="Fruit Selection")
file_path = easygui.fileopenbox("Select a file to open")
easygui.msgbox(f"You selected: {file_path}", title="File Selection")
customtkinter是一個用于創建現代、美觀GUI的庫,適合需要高級外觀和交互的應用程序。
安裝customtkinter:
pip install customtkinter
示例代碼(登錄系統):
import customtkinter as ctk
def login():
print("Login Successful")
root = ctk.CTk()
root.geometry("500x350")
root.title("Login System")
frame = ctk.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=True)
label = ctk.CTkLabel(master=frame, text="Login System", font=("Roboto", 24))
label.pack(pady=12, padx=10)
entry_username = ctk.CTkEntry(master=frame, placeholder_text="Username")
entry_username.pack(pady=12, padx=10)
entry_password = ctk.CTkEntry(master=frame, placeholder_text="Password", show="*")
entry_password.pack(pady=12, padx=10)
button = ctk.CTkButton(master=frame, text="Login", command=login)
button.pack(pady=12, padx=10)
checkbox = ctk.CTkCheckBox(master=frame, text="Remember Me")
checkbox.pack(pady=12, padx=10)
root.mainloop()
在開始開發之前,確保你已經安裝了Python和必要的開發工具。
安裝Python:
sudo apt update
sudo apt install python3 python3-pip
安裝虛擬環境(可選):
sudo apt install python3-venv
python3 -m venv venv
source venv/bin/activate
安裝開發工具:
sudo apt install python3-dev
通過以上步驟,你可以在Ubuntu上輕松開始Python圖形界面的開發。選擇合適的庫(如EasyGUI或customtkinter),并按照示例代碼進行操作,即可創建出功能齊全的GUI應用程序。