Ubuntu上Python图形界面开发可选Tkinter、PyQt5、customtkinter、PyGObject、Kivy、wxPython等库,分别适用于快速原型、复杂应用、现代美观、Linux原生、触摸屏及跨平台桌面,需按项目需求选择。
在Ubuntu系统上搞Python图形界面开发,其实选择挺多的。不同的库适合不同的场景,从快速验证原型的玩具,到能扛起复杂业务的生产力工具,都能找到对应的方案。下面就把几个主流的选择拆开来说说,包括怎么装、怎么用、以及它们各自擅长干什么。

长期稳定更新的攒劲资源: >>>点此立即查看<<<
先明确一点:Python生态里GUI库的丰富程度,超过很多人的想象。但选库的时候,不用太纠结“哪个最好”——关键是看你的项目有多大、对界面外观的要求有多高、以及目标用户是谁。
Tkinter是Python官方自带的,装上Python就能用,Ubuntu默认也包含。它提供基本的控件,比如标签、按钮、输入框,写个小工具、快速验证想法非常方便。
import tkinter as tkroot = tk.Tk()root.title("我的第一个Tkinter窗口")label = tk.Label(root, text="Hello, Tkinter!")label.pack(pady=20)root.mainloop()PyQt5是Qt框架的Python绑定,控件非常丰富——表格、树形视图、各种布局管理器,还有Qt Designer这个可视化设计工具,可以拖拽生成界面。做专业级桌面应用,它几乎是首选。
pip3 install pyqt5import sysfrom PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidgetclass MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("PyQt5示例")self.setGeometry(100, 100, 300, 200)# 主部件与布局central_widget = QWidget()layout = QVBoxLayout()# 标签与按钮label = QLabel("欢迎使用PyQt5!")button = QPushButton("点击我")button.clicked.connect(lambda: label.setText("按钮被点击了!"))# 添加控件到布局layout.addWidget(label)layout.addWidget(button)central_widget.setLayout(layout)self.setCentralWidget(central_widget)app = QApplication(sys.argv)window = MainWindow()window.show()sys.exit(app.exec_())如果你觉得Tkinter默认的界面太“复古”,customtkinter是个不错的选择。它基于Tkinter,但提供了圆角控件、主题切换、动画效果,能做出很现代的外观。
pip install customtkinterimport customtkinter as ctkdef login():print("登录成功!")# 设置主题与外观ctk.set_appearance_mode("dark")# 主题:light/dark/systemctk.set_default_color_theme("blue")# 主题颜色:blue/green/dark-blueroot = ctk.CTk()root.geometry("400x300")root.title("登录系统")# 主框架frame = ctk.CTkFrame(root)frame.pack(pady=40, padx=50, fill="both", expand=True)# 标签label = ctk.CTkLabel(frame, text="用户登录", font=("Arial", 24))label.pack(pady=20)# 输入框entry_username = ctk.CTkEntry(frame, placeholder_text="用户名")entry_username.pack(pady=10)entry_password = ctk.CTkEntry(frame, placeholder_text="密码", show="*")entry_password.pack(pady=10)# 复选框与按钮checkbox = ctk.CTkCheckBox(frame, text="记住我")checkbox.pack(pady=10)button = ctk.CTkButton(frame, text="登录", command=login)button.pack(pady=20)root.mainloop()如果你想让应用跑在GNOME桌面上,看起来就像系统原生的一部分,PyGObject是最佳选择。它是GTK的Python绑定,支持主题定制和系统集成。
sudo apt updatesudo apt install python3-gi gir1.2-gtk-3.0import gigi.require_version("Gtk", "3.0")from gi.repository import Gtkclass MainWindow(Gtk.Window):def __init__(self):super().__init__(title="PyGObject示例")self.set_default_size(250, 150)# 按钮button = Gtk.Button(label="点击我")button.connect("clicked", self.on_button_clicked)self.add(button)def on_button_clicked(self, widget):print("按钮被点击了!")win = MainWindow()win.connect("destroy", Gtk.main_quit)win.show_all()Gtk.main()Kivy基于OpenGL,支持多点触控和丰富的动画效果。它的核心理念是“一次编写,到处运行”,特别适合做移动端或者触摸屏的桌面应用,比如教育类游戏、交互式工具。
pip3 install kivyfrom kivy.app import Appfrom kivy.uix.label import Labelclass MyApp(App):def build(self):return Label(text="Hello, Kivy!", font_size=30)if __name__ == "__main__":MyApp().run()wxPython的思路是尽量使用操作系统原生的控件——在Windows上就像Windows程序,在macOS上就像macOS程序。如果你追求“原汁原味”的平台体验,又不愿意用Qt,它是个好选择。
sudo apt install python3-wxgtk4.0import wxapp = wx.App(False)frame = wx.Frame(None, wx.ID_ANY, "wxPython示例", size=(300, 200))panel = wx.Panel(frame, wx.ID_ANY)label = wx.StaticText(panel, wx.ID_ANY, "Hello, wxPython!", pos=(100, 80))frame.Show(True)app.MainLoop()侠游戏发布此文仅为了传递信息,不代表侠游戏网站认同其观点或证实其描述