sairate 2cd753d3d9 feat(第7讲 字母卡牌): 实现记忆游戏并添加开始界面
- 新增 jiyi.py 文件,实现字母翻牌记忆游戏功能
- 添加 youxijiemian.py 文件,创建游戏开始界面
- 使用 turtle 和 tkinter 模块分别实现游戏和界面
- 支持选择不同难度的游戏模式
2025-06-29 09:06:19 +08:00

50 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'''【课后作业要求】
生肖计算助手
要求:
1.窗口内有标签、单行文本输入框、提交按钮
2.输入正确的年份1900-2100后会弹出对话框显示生肖
3.输入的年份不在范围内或者不是数字格式,会弹出错误对话框
注意判断生肖的函数check(year)已经预置好,可以直接调用;组件的位置可以自行调整,合理美观即可。'''
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.geometry("300x200+600+300")
root.title("生肖计算助手")
# 预置内容,请勿改动
def check(year):
sx = ["", "", "", "", "", "", "", "", "", "", "", ""]
idx = (year - 1900) % 12
return sx[idx]
def click():
year = entry_year.get()
if not year.isdigit():
messagebox.showerror("输入错误", "请输入有效的年份数值")
year = int(year)
if year < 1900 or year > 2100:
messagebox.showerror("输入错误", "请输入有效的年份数值")
result = check(year)
messagebox.showinfo("属相", f"您的生肖是:{result}")
label_year = tk.Label(root, text='出生年份:')
label_year.grid(row=0, column=0, padx=20, pady=50)
entry_year = tk.Entry(root)
entry_year.grid(row=0, column=1, pady=50)
btn = tk.Button(root, text='提交', command=click)
btn.grid(row=1, columnspan=2, padx=(60, 0))
root.mainloop()