在Ubuntu上进行Python GUI开发,你可以选择多种图形用户界面(GUI)库。以下是一些流行的Python GUI库以及如何在Ubuntu上安装和使用它们的简要指南:
Tkinter:
sudo apt update
sudo apt install python3-tk
import tkinter as tk
root = tk.Tk()
root.title("Hello World")
label = tk.Label(root, text="Hello, Tkinter!")
label.pack()
root.mainloop()
PyQt5:
pip3 install pyqt5
from PyQt5.QtWidgets import QApplication, QLabel
app = QApplication([])
label = QLabel('Hello, PyQt5!')
label.show()
app.exec_()
Kivy:
pip3 install kivy
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def build(self):
return Button(text='Hello, Kivy!')
if __name__ == '__main__':
MyApp().run()
wxPython:
pip3 install wxPython
import wx
app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "Hello World")
panel = wx.Panel(frame, wx.ID_ANY)
label = wx.StaticText(panel, wx.ID_ANY, "Hello, World!", (50, 50))
frame.Show(True)
app.MainLoop()
GTK+:
sudo apt update
sudo apt install python3-gi gir1.2-gtk-3.0
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Hello World")
self.set_default_size(200, 100)
label = Gtk.Label(label="Hello, World!")
self.add(label)
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
选择哪个库取决于你的需求和个人喜好。Tkinter适合快速开发和简单的界面,PyQt5和PySide2提供了更多的功能和更好的外观。Kivy适合需要多点触控支持的应用程序,wxPython和Gtk则适合需要原生感觉的应用程序。