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

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
雷峰网
雷峰网
T
Tailwind CSS Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Blog of Author Tim Ferriss
S
Schneier on Security
C
CERT Recently Published Vulnerability Notes
Help Net Security
Help Net Security
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
I
InfoQ
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
G
GRAHAM CLULEY
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
I
Intezer
大猫的无限游戏
大猫的无限游戏
AWS News Blog
AWS News Blog
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
Spread Privacy
Spread Privacy
博客园_首页
宝玉的分享
宝玉的分享
量子位
T
Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Security Latest
Security Latest
C
Cybersecurity and Infrastructure Security Agency CISA
SecWiki News
SecWiki News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - Franky
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
T
Tenable Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
The Register - Security
The Register - Security
V2EX - 技术
V2EX - 技术
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Last Week in AI
Last Week in AI
L
LangChain Blog
T
Tor Project blog
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客

博客园 - 箫笛

Tkinter - Entry 输入框组件 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 可以使你的代码组织更清晰,并且在应用规模扩大时保持良好的可扩展性。