医院HIS详细设计说明书:业务流程、数据库与接口设计要点
2026/10/14 10:54:54
Python不仅能写脚本,还能做桌面应用。本文对比Tkinter、PyQt、Flet三种GUI方案,并实战开发一个工具。
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()
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基于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)| 特性 | Tkinter | PyQt | Flet |
|---|---|---|---|
| 安装 | 内置 | pip | pip |
| 外观 | 一般 | 专业 | 现代 |
| 学习曲线 | 平缓 | 陡峭 | 平缓 |
| 跨平台 | 是 | 是 | 是 |
| 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适合现代化跨平台应用。选择取决于项目需求。