惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cisco Blogs
V
Vulnerabilities – Threatpost
G
GRAHAM CLULEY
N
News | PayPal Newsroom
NISL@THU
NISL@THU
雷峰网
雷峰网
J
Java Code Geeks
Latest news
Latest news
aimingoo的专栏
aimingoo的专栏
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
Spread Privacy
Spread Privacy
V2EX - 技术
V2EX - 技术
S
Schneier on Security
K
Kaspersky official blog
Recent Announcements
Recent Announcements
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
S
SegmentFault 最新的问题
Security Archives - TechRepublic
Security Archives - TechRepublic
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Scott Helme
Scott Helme
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
量子位
A
Arctic Wolf

博客园 - 箫笛

Tkinter - Button 组件 Tkinter - Label 组件 Tkinter - tk 变量 Tkinter - 事件与绑定 Tkinter - 几何管理器 Tkinter - 核心概念 Tkinter - 介绍 Python 编程 - 条件表达式 Python 编程 - 星号下划线参数释义 shell 编程 - shell 脚本的交互方式 Python insall - macOS 系统安装python的几种方式 Miniconda - Python 环境管理工具 Tkinter - Python GUI 开发 Python 编程 - 下划线命名的区别 Python 编程 - 元类编程 Python 编程 - 多重继承与MRO Python 编程 - 描述符协议 Python 编程 - 类型注解 Python 编程 - 生成器表达式 Python 编程 - 集合 Python 编程 - 装饰器 Python 编程 - 闭包 Python 编程 - 列表推导式 Python 编程 - 函数式编程 Python 编程 - lambda 函数 Python 编程 - 文件操作 Python 编程 - 输入与输出 Python 编程 - 面向对象编程 Python 编程 - 元组(tuple) Python 编程 - 字符串(str) Python 编程 - 字典(dict) Python 编程 - 列表(list) Python 编程 - 数据类型和数据结构 Python 编程 - 语句 Python 编程 - 函数 windows - WSL 的安装与使用 shell编程 - dialog 程序使用指南 FE Team - 如何做好前端代码审查 git 提交的撤销和恢复 React15 - redux-saga 如何在saga中实现轮询接口调用? React15 - React CSS Modules BEM命名实践 React15 - React 15 中 componentWillReceiveProps 为什么会多次调用, 同时componentDidUpdate 也会多次调用? React15 - React15类组件多次执行render方法的原因? React15 - React15应用中代码逻辑复用方案 React15 - React状态同步问题解决 React15 - React 15 中 React.pureComponent 的使用场景 React15 - React 15应用在页面渲染时会多次执行类组件的render 函数的原因 React15 - React 15 中能用 componetDidUpdate 代替 componentWillReceiveProps 吗? React15 - React 15 生命周期函数详解 React15 - 如何在React 15中实现自定义的事件订阅与发送(例如组件间通信) React15 - React15应用中的事件订阅和发送机制 React15 - CSS中的BEM规范 React15 - React CSS Modules BEM命名实践 React15 - 写sass 样式文件,嵌套的结构好,还是扁平的结构好? React15 - sass 中 @mixin 和 @extend 的区别是什么? React15 - React 15 应用 如何使用Css moudules 方式进行模块化开发 React15 - React15应用Sass使用指南 React15 - React 15 应用如何进行性能优化?
Tkinter - 快速开始
箫笛 · 2026-07-09 · via 博客园 - 箫笛

安装

Tkinter 随 Python 一同发行。验证其是否可用:

终端

python -c "import tkinter; print(tkinter.TkVersion)"
# 应输出:8.6

⚠️ Linux 用户

在 Ubuntu/Debian 上,运行:sudo apt-get install python3-tk

你的第一个窗口

每个 Tkinter 应用程序都遵循相同的三步模式:

  1. 创建根窗口 —— tk.Tk()
  2. 在窗口内添加控件
  3. 启动事件循环 —— root.mainloop()

first_window.py

import tkinter as tk

root = tk.Tk()
root.title("我的第一个应用")
root.geometry("400x300")  # 宽度 x 高度
root.mainloop()

完整应用:温度转换器

temp_converter.py

import tkinter as tk
from tkinter import ttk

class TempConverter(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("温度转换器")
        self.resizable(False, False)
        self.celsius    = tk.StringVar(value="0")
        self.fahrenheit = tk.StringVar(value="32")
        self._build_ui()
        self.celsius.trace_add("write", self._from_c)
        self.fahrenheit.trace_add("write", self._from_f)

    def _build_ui(self):
        f = ttk.Frame(self, padding=20)
        f.pack()
        ttk.Label(f, text="°C").grid(row=0, column=1)
        ttk.Entry(f, textvariable=self.celsius,    width=10).grid(row=0, column=0)
        ttk.Label(f, text=" = ").grid(row=0, column=2)
        ttk.Entry(f, textvariable=self.fahrenheit, width=10).grid(row=0, column=3)
        ttk.Label(f, text="°F").grid(row=0, column=4)

    def _from_c(self, *_):
        try: self.fahrenheit.set(f"{float(self.celsius.get())*9/5+32:.2f}")
        except: pass

    def _from_f(self, *_):
        try: self.celsius.set(f"{(float(self.fahrenheit.get())-32)*5/9:.2f}")
        except: pass

TempConverter().mainloop()

💡 基于类的应用

继承 tk.Tk 可以使你的代码组织更清晰,并且在应用规模扩大时保持良好的可扩展性。