溫馨提示×

Ubuntu中Python圖形界面如何開發

小樊
73
2025-02-28 11:56:52
欄目: 編程語言

在Ubuntu中開發Python圖形界面(GUI)有多種選擇,以下是一些常用的庫和方法:

Tkinter

Tkinter是Python的標準GUI庫,適合初學者。它簡單易用,無需額外安裝,且功能強大。

安裝與啟動

import tkinter as tk

root = tk.Tk()
root.title("我的第一個GUI應用")
root.geometry("400x300")
root.mainloop()

添加控件

label = tk.Label(root, text="歡迎來到Python GUI世界!")
label.pack()

entry = tk.Entry(root)
entry.pack()

button = tk.Button(root, text="點擊我")
button.pack()

PyQt5

PyQt5是一個功能強大的庫,支持跨平臺運行,適合開發復雜的桌面應用程序。

安裝

pip install PyQt5

示例代碼

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox

app = QApplication(sys.argv)

window = QWidget()
window.setWindowTitle('我的第一個PyQt窗口')
window.setGeometry(100, 100, 300, 200)
window.show()

def show_message():
    QMessageBox.information(window, '消息', '你點擊了按鈕!')

button = QPushButton('點擊我', window)
button.clicked.connect(show_message)
button.move(100, 80)

sys.exit(app.exec_())

customtkinter

customtkinter是一個用于創建現代、美觀GUI的庫。

安裝

pip install customtkinter

示例代碼

import customtkinter as ctk

ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")

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和pip??梢允褂靡韵旅畎惭bpip:

sudo apt update
sudo apt install python3-pip

總結

以上是幾種在Ubuntu上開發Python圖形界面的方法,你可以根據自己的需求選擇合適的庫進行開發。無論是使用Tkinter進行簡單的GUI開發,還是使用PyQt5和customtkinter創建復雜的桌面應用程序,這些庫都能提供豐富的控件和功能,幫助你輕松實現圖形用戶界面。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女