☰
HoRain云--Python GUI开发:Tkinter、PyQt与Flet
2026/10/14 10:09:16 网站建设 项目流程

Python不仅能写脚本,还能做桌面应用。本文对比Tkinter、PyQt、Flet三种GUI方案,并实战开发一个工具。

一、Tkinter入门

Tkinter是标准库,无需安装。

python

复制

下载

import tkinter as tk from tkinter import ttk, messagebox root = tk.Tk() root.title("我的应用") root.geometry("400x300") label = tk.Label(root, text="Hello", font=("Arial", 16)) label.pack(pady=10) entry = tk.Entry(root) entry.pack(pady=5) def on_click(): messagebox.showinfo("提示", f"你输入了: {entry.get()}") btn = tk.Button(root, text="确定", command=on_click) btn.pack(pady=10) root.mainloop()

二、布局管理

python

复制

下载

# pack tk.Label(root, text="A").pack(side="top") tk.Label(root, text="B").pack(side="left") # grid tk.Label(root, text="用户名").grid(row=0, column=0) tk.Entry(root).grid(row=0, column=1) tk.Label(root, text="密码").grid(row=1, column=0) tk.Entry(root, show="*").grid(row=1, column=1) # place tk.Label(root, text="绝对定位").place(x=10, y=10)

三、常用控件

python

复制

下载

# 复选框 var = tk.BooleanVar() tk.Checkbutton(root, text="同意", variable=var).pack() # 单选框 choice = tk.StringVar(value="A") tk.Radiobutton(root, text="A", variable=choice, value="A").pack() tk.Radiobutton(root, text="B", variable=choice, value="B").pack() # 下拉框 combo = ttk.Combobox(root, values=["Python", "Java", "Go"]) combo.pack() # 列表 listbox = tk.Listbox(root) for item in ["a", "b", "c"]: listbox.insert(tk.END, item) listbox.pack() # 文本 text = tk.Text(root, height=5) text.pack()

四、PyQt5

python

复制

下载

from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox) import sys class MyWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQt示例") self.resize(400, 300) layout = QVBoxLayout() self.label = QLabel("Hello") self.input = QLineEdit() btn = QPushButton("点击") btn.clicked.connect(self.on_click) layout.addWidget(self.label) layout.addWidget(self.input) layout.addWidget(btn) self.setLayout(layout) def on_click(self): QMessageBox.information(self, "提示", self.input.text()) app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_())

五、Flet(现代化方案)

Flet基于Flutter,支持Web和桌面。

python

复制

下载

import flet as ft def main(page: ft.Page): page.title = "Flet应用" page.theme_mode = ft.ThemeMode.LIGHT def on_click(e): page.add(ft.Text(f"Hello, {name.value}!")) page.update() name = ft.TextField(label="姓名", width=300) btn = ft.ElevatedButton("提交", on_click=on_click) page.add( ft.Column([ ft.Text("欢迎", size=24, weight="bold"), name, btn, ], alignment=ft.MainAxisAlignment.CENTER) ) ft.app(target=main)

六、三种方案对比

特性TkinterPyQtFlet
安装内置pippip
外观一般专业现代
学习曲线平缓陡峭平缓
跨平台是是是
Web支持否否是
适合小工具桌面软件快速开发

七、实战:文件批量处理工具

python

复制

下载

import tkinter as tk from tkinter import filedialog, messagebox from pathlib import Path import os class FileTool: def __init__(self, root): self.root = root root.title("文件批量重命名") root.geometry("500x300") tk.Button(root, text="选择目录", command=self.choose_dir).pack(pady=10) self.dir_label = tk.Label(root, text="未选择") self.dir_label.pack() tk.Label(root, text="前缀:").pack() self.prefix = tk.Entry(root) self.prefix.insert(0, "file_") self.prefix.pack() tk.Button(root, text="开始重命名", command=self.rename).pack(pady=10) self.directory = None def choose_dir(self): self.directory = filedialog.askdirectory() self.dir_label.config(text=self.directory) def rename(self): if not self.directory: messagebox.showwarning("提示", "请先选择目录") return prefix = self.prefix.get() files = sorted(Path(self.directory).iterdir()) for i, f in enumerate(files, 1): if f.is_file(): new_name = f.parent / f"{prefix}{i:04d}{f.suffix}" f.rename(new_name) messagebox.showinfo("完成", f"重命名 {len(files)} 个文件") root = tk.Tk() app = FileTool(root) root.mainloop()

八、打包发布

PyInstaller:

bash

复制

下载

pip install pyinstaller pyinstaller --onefile --windowed myapp.py

九、总结

Tkinter适合快速小工具,PyQt适合专业桌面软件,Flet适合现代化跨平台应用。选择取决于项目需求。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询