python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python Tkinter 模块

深入Python Tkinter 模块

作者:小羊苏八

本文介绍Python Tkinter模块,强调其轻量、跨平台、无依赖的优势,适用于快速开发无需复杂环境配置的桌面工具,通过布局、事件回调、样式美化等核心技巧,结合PyInstaller打包,实现从脚本到可交付程序的完整流程,适合制作简单实用的小工具,感兴趣的朋友跟随小编一起看看吧

一、为什么要写 Tkinter

当你想给同事或客户交付一个“双击就能跑”的小工具,却不想让他们先装 Anaconda、再配环境、再命令行启动时,Tkinter 会成为最轻量的答案。它随 Python 官方发行版附带,跨 Windows、macOS、Linux 零依赖;语法贴近 Python 本身,学习曲线几乎等同写脚本;又因为底层调的是系统原生控件,界面朴素却稳定。本文将带你走完“认知—布局—交互—美化—打包”的完整闭环,所有代码均可直接复制运行,每行都有白话拆解。

二、最小可运行示例:Hello World 不是终点,而是起点

打开任意编辑器,新建 hello.py,键入以下五行:

import tkinter as tk
root = tk.Tk()
root.title("你好,Tkinter")
tk.Label(root, text="Hello World", font=("微软雅黑", 24)).pack()
root.mainloop()

运行后弹出一个 300×200 左右的窗体,中央显示大号“Hello World”。
逐行拆解:

三、布局三板斧:pack、grid、place

控件有了,下一步决定“放哪”。Tkinter 提供三种布局策略,场景不同,取舍清晰。

pack:沿一条边“顺次堆叠”,适合工具条、按钮条这类线性结构。
示例:把两个按钮左右排布。

import tkinter as tk
root = tk.Tk()
btn_a = tk.Button(root, text="A")
btn_b = tk.Button(root, text="B")
btn_a.pack(side="left", padx=10, pady=10)
btn_b.pack(side="left", padx=10, pady=10)
root.mainloop()

side 可选 top、bottom、left、right,padx/pady 控制外边距。

grid:表格思维,行列坐标定位,适合表单、棋盘、仪表盘。
示例:登录表单两行两列。

import tkinter as tk
root = tk.Tk()
tk.Label(root, text="用户名").grid(row=0, column=0, sticky="e")
tk.Entry(root).grid(row=0, column=1)
tk.Label(root, text="密码").grid(row=1, column=0, sticky="e")
tk.Entry(root, show="*").grid(row=1, column=1)
root.mainloop()

sticky 类比 CSS 的 align,"e" 表示东(右对齐),"nsew" 表示四向拉伸。

place:绝对坐标,像素级精准,适合游戏画布、可视化仪表。
示例:把按钮放在 (50, 50)。

btn = tk.Button(root, text="Drag me")
btn.place(x=50, y=50)

一旦窗口拉伸,place 控件不会自动跟随,需手写 <Configure> 事件做重算,一般很少用。

四、事件与回调:让按钮“响”起来

GUI 的本质是“事件驱动”。用户点一下,系统产生事件,程序注册回调函数。最简单的绑定方式是 command= 参数。

import tkinter as tk
from tkinter import messagebox
def on_click():
    messagebox.showinfo("提示", "按钮被点了")
root = tk.Tk()
tk.Button(root, text="点我", command=on_click).pack(pady=20)
root.mainloop()

如果想捕获键盘、鼠标移动或窗口关闭,可用通用绑定:

root.bind("<KeyPress>", lambda e: print(e.keysym))
canvas.bind("<B1-Motion>", draw_line)
root.protocol("WM_DELETE_WINDOW", ask_before_quit)

五、实战案例:秒表 + 文件批量重命名器

把上面知识点串起来,做一个真正可交付的小工具。

需求拆解
用户打开程序,界面上半区是秒表(开始/暂停/复位),下半区可批量选择文件夹,对其下所有图片按日期重命名。功能虽杂,但共用同一个根窗口。

目录结构
project/
├── stopwatch.py # 秒表组件
├── renamer.py # 重命名组件
└── main.py # 组装入口

秒表组件 stopwatch.py

import time
import tkinter as tk
class Stopwatch(tk.Frame):
    def __init__(self, master):
        super().__init__(master)
        self.time = 0
        self.running = False
        self.label = tk.Label(self, text="00:00.00", font=("Helvetica", 40))
        self.label.pack()
        btn_bar = tk.Frame(self)
        btn_bar.pack()
        tk.Button(btn_bar, text="开始", command=self.start).pack(side="left")
        tk.Button(btn_bar, text="暂停", command=self.pause).pack(side="left")
        tk.Button(btn_bar, text="复位", command=self.reset).pack(side="left")
        self.update_clock()
    def start(self):
        self.running = True
    def pause(self):
        self.running = False
    def reset(self):
        self.running = False
        self.time = 0
        self.label.config(text="00:00.00")
    def update_clock(self):
        if self.running:
            self.time += 0.1
            mins, secs = divmod(self.time, 60)
            self.label.config(text=f"{int(mins):02d}:{secs:05.2f}")
        self.after(100, self.update_clock)

说明:

4.重命名组件 renamer.py

import os, datetime
from tkinter import filedialog, messagebox, ttk
import tkinter as tk
class Renamer(tk.Frame):
    def __init__(self, master):
        super().__init__(master)
        self.path = tk.StringVar()
        tk.Label(self, text="文件夹:").pack(anchor="w")
        tk.Entry(self, textvariable=self.path, width=50).pack(fill="x")
        tk.Button(self, text="浏览", command=self.browse).pack(anchor="e")
        tk.Button(self, text="开始重命名", command=self.rename).pack(pady=10)
        self.progress = ttk.Progressbar(self, mode='determinate')
        self.progress.pack(fill="x")
    def browse(self):
        dir_ = filedialog.askdirectory()
        if dir_:
            self.path.set(dir_)
    def rename(self):
        folder = self.path.get()
        if not folder:
            messagebox.showwarning("提示", "请先选择文件夹")
            return
        files = [f for f in os.listdir(folder) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
        self.progress["maximum"] = len(files)
        for idx, name in enumerate(files, 1):
            full = os.path.join(folder, name)
            t = datetime.datetime.fromtimestamp(os.path.getmtime(full))
            new_name = t.strftime("%Y%m%d_%H%M%S") + os.path.splitext(name)[1]
            os.rename(full, os.path.join(folder, new_name))
            self.progress["value"] = idx
            self.update_idletasks()
        messagebox.showinfo("完成", f"已处理 {len(files)} 张图片")

说明:

5.主窗口 main.py

import tkinter as tk
from stopwatch import Stopwatch
from renamer import Renamer
root = tk.Tk()
root.title("多功能小工具 1.0")
root.geometry("400x400")
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True)
tab1 = tk.Frame(notebook)
tab2 = tk.Frame(notebook)
notebook.add(tab1, text="秒表")
notebook.add(tab2, text="图片重命名")
Stopwatch(tab1).pack(expand=True)
Renamer(tab2).pack(fill="both", expand=True, padx=10, pady=10)
root.mainloop()

说明:

六、样式进阶:让“原生”不再“土味”

Tkinter 默认观感像 90 年代,好在 8.6 之后集成 ttk 主题引擎,一行代码换肤:

from tkinter import ttk
style = ttk.Style()
style.theme_use("clam")  # 可选 clam, alt, default, classic, vista

如果想再精致,可自定义配色:

style.configure("TButton", foreground="#ffffff", background="#0078d4", font=("Segoe UI", 10))
style.map("TButton", background=[("active", "#106ebe")])

图标与字体可用相对路径打包,配合 PyInstaller 一键生成 exe,下文详述。

七、打包与交付:PyInstaller 的三行命令

在项目根目录打开终端:

pip install pyinstaller
pyinstaller -F -w -i icon.ico main.py

参数解读:
-F 单文件,-w 去控制台,-i 指定图标。生成的 dist/main.exe 可拷到任何未装 Python 的 Windows 机器双击运行。Linux 与 macOS 同理,只需把 -F 换成 -D 以便依赖库分离,减少体积。

八、常见坑与调试技巧

from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)

调试打印:IDE 的 console 可能看不到 print,可把日志写到 Text 控件或文件。

九、结语:用 200 行代码撬动生产力

本文从最小示例到双选项卡多功能工具,再到打包发布,完整示范了 Tkinter 的“小而美”哲学。它当然不是 Qt、WxPython 的对手,却在“随装随用、脚本即应用”场景里无可替代。下次当你写了一个数据分析脚本,想给运营同事一个按钮就能跑,不妨用本文的模板套壳,十分钟交差。愿你在 Python 的极简 GUI 之路上,跑得飞快,亦不忘乐在其中。

到此这篇关于Python Tkinter 模块的文章就介绍到这了,更多相关Python Tkinter 模块内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文