用 Tkinter 实现一个简单的干支纪年计算器

背景

TkDocs tutorial 里介绍了 Tkinter\text{Tkinter} Tkinter,其中有 A First (Real) Example 一文,这篇文章里有一个使用 Tkinter\text{Tkinter} Tkinter 生成图形化界面的简单例子。我想在那篇文章的基础上实战一下,于是想到可以写一个公元年份 ➡️ 干支纪年(其实严格来讲,一个公元年份并不能和一个干支纪年完全对应)的简易计算器。

正文

如何将公元年份转化为干支纪年

天干有如下 1010 10 个

地支有如下 1212 12 个

计算对应的天干

公元 19841984 1984 年是甲子年。由于天干的数量是 1010 10,所以可以这样计算天干 ⬇️

python 复制代码
def calculate_tian_gan(specified_year):
    tian_gan_candidates = "甲乙丙丁戊己庚辛壬癸"
    candidates_cnt = len(tian_gan_candidates)
    last_digit = specified_year % candidates_cnt
    return tian_gan_candidates[(last_digit + 6) % candidates_cnt]

计算对应的地支

天干的数量( 1010 10)和地支的数量( 1212 12)都是( 6060 60)的约数,所以对公元年份 yy y 和 y+60y + 60 y+60 而言(假设 y>0y\gt0 y>0),它们对应的干支纪年是一样的。

公元 19841984 1984 年是甲子年,那么公元 184184 184 年也是甲子年( 1984−60×30=1841984-60\times 30=184 1984−60×30=184),那么公元 44 4 年也是甲子年( 184−60×3=4184-60\times 3=4 184−60×3=4),所以可以这样计算地支 ⬇️

python 复制代码
def calculate_di_zhi(specified_year):
    di_zhi_candidates = "子丑寅卯辰巳午未申酉戌亥"
    candidates_cnt = len(di_zhi_candidates)
    remaider = specified_year % candidates_cnt
    return di_zhi_candidates[(remaider + 8) % candidates_cnt]

完整的代码

A First (Real) Example 一文中有使用 Tkinter\text{Tkinter} Tkinter 生成图形化界面的简单例子。在它的基础上,可以写出以下 Python3\text{Python3} Python3 代码

python 复制代码
from tkinter import *
from tkinter import ttk

def calculate():
    try:
        specified_year = year.get()
        if (specified_year <= 0):
            result.set("请输入大于0的公元年份!")
            return
        tian_gan = calculate_tian_gan(specified_year)
        di_zhi = calculate_di_zhi(specified_year)
        result.set(tian_gan + di_zhi)
    except ValueError:
        pass

def calculate_tian_gan(specified_year):
    tian_gan_candidates = "甲乙丙丁戊己庚辛壬癸"
    candidates_cnt = len(tian_gan_candidates)
    last_digit = specified_year % candidates_cnt
    return tian_gan_candidates[(last_digit + 6) % candidates_cnt]

def calculate_di_zhi(specified_year):
    di_zhi_candidates = "子丑寅卯辰巳午未申酉戌亥"
    candidates_cnt = len(di_zhi_candidates)
    remaider = specified_year % candidates_cnt
    return di_zhi_candidates[(remaider + 8) % candidates_cnt]

root = Tk()
root.title("公元年份对应干支纪年计算器")

mainframe = ttk.Frame(root, padding=(3, 3, 12, 12))
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))

year = IntVar()
year_entry = ttk.Entry(mainframe, width=7, textvariable=year)
year_entry.grid(column=2, row=1, sticky=(W, E))

ttk.Button(mainframe, text="对应的干支纪年:", command=calculate).grid(column=1, row=2, sticky=W)

ttk.Label(mainframe, text="公元年份:").grid(column=1, row=1, sticky=W)

result = StringVar()
ttk.Label(mainframe, textvariable=result).grid(column=2, row=2, sticky=(W, E))

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
mainframe.columnconfigure(2, weight=1)
for child in mainframe.winfo_children(): 
    child.grid_configure(padx=5, pady=5)

year_entry.focus()
root.bind("<Return>", calculate)

root.mainloop()

运行

请将上一小节展示的完整代码保存为 ganzhi.py。使用如下命令可以运行 ganzhi.py

bash 复制代码
python3 ganzhi.py

运行效果如下

我们输入一个正数,例如当前年份 20262026 2026,效果如下

再用其他年份验证一下(例如 19841984 1984 年)

运行结果符合预期

参考资料

相关推荐
AC赳赳老秦1 小时前
OpenClaw+MySQL 深度应用:自动生成建表语句、索引优化建议与数据迁移脚本
开发语言·数据库·人工智能·python·mysql·算法·openclaw
西贝爱学习1 小时前
旅游推荐数据集.csv
python·数据集·旅游
qcx231 小时前
【AI Daily 2026-06-05】「持续迭代」已成为 2026 年 Agent 研究的核心命题
人工智能·python·agent
2601_961194021 小时前
2026四级词汇闪过电子版|高频词+真题词速记PDF
数据库·python·django·pdf·pygame
SunnyRivers2 小时前
Python 中的类型安全:Pydantic vs. Data Classes vs. Annotations vs. TypedDicts
python·pydantic·类型安全·dataclass·typedict
一晌小贪欢2 小时前
第19节:地理空间分析——使用 Geopandas 绘制热力地图
开发语言·python·数据分析·pandas·数据可视化
Wonderful U2 小时前
基于Python+Django+MySQL构建个人任务管理系统:告别零散记录,实现高效日程管理
python·mysql·django
墨痕无声2 小时前
网络基础知识
后端
TickDB2 小时前
支持 MCP 的金融行情数据源怎么选:实时行情、财务数据和交易 API 的工程边界
python·websocket·mcp·行情数据 api