- 导入必要库:
import tkinter as tk
import re
from tkinter import messagebox
-
创建主窗口:使用
tk.Tk()
创建主窗口,将其赋值给root
变量。 -
设置主窗口标题:使用
root.title("星座识别器")
为主窗口设置标题为"星座识别器"。 -
设置窗口大小:使用
root.geometry("300x200")
设置主窗口的大小为300x200像素。
root = tk.Tk()
root.title("星座识别器")
root.geometry("300x200")
-
创建标签:使用
tk.Label()
创建一个标签控件,显示文本"出生日期:",并设置文本在标签中居中显示。 -
显示标签:使用
label_birth_date.pack(pady=10)
将标签显示在窗口上,并设置垂直方向上的内边距为10像素,使其居中显示。 -
创建输入框:使用
tk.Entry()
创建一个输入框控件,用于接收用户输入的出生日期。 -
显示输入框:使用
entry_birth_date.pack()
将输入框显示在窗口上。
label_birth_date = tk.Label(root, text="出生日期:", justify=tk.CENTER)
label_birth_date.pack(pady=10)
entry_birth_date = tk.Entry(root)
entry_birth_date.pack()
-
创建函数
send_birth_date()
:该函数用于处理用户点击"确认"按钮后的操作。 -
获取用户输入:使用
entry_birth_date.get()
获取用户在输入框中输入的出生日期。 -
检查日期格式:使用正则表达式检查用户输入的出生日期是否符合特定格式(xxxx/xx/xx),如果格式不正确,弹出错误消息框。
-
识别星座:调用
identify_constellation()
函数根据用户输入的日期来识别对应的星座。 -
显示结果:将识别到的星座显示在名为
result_label
的标签上。
def send_birth_date():
birth_date = entry_birth_date.get()
pattern = r'^\d{4}/\d{2}/\d{2}$'
if re.match(pattern, birth_date):
constellation = identify_constellation(birth_date)
result_label.config(text=f"您的星座是:{constellation}")
else:
messagebox.showerror("错误", "请输入正确的出生日期格式(例如:xxxx/xx/xx)")
-
创建函数
identify_constellation(birth_date)
:该函数用于根据日期来识别星座。 -
获取日期信息:将用户输入的出生日期按照年、月、日的形式提取出来。
-
识别星座:根据出生日期的年份和月份来判断对应的星座,并返回对应的星座名。
def identify_constellation(birth_date):
year, month, day = map(int, birth_date.split('/'))
if (month == 3 and day >= 21) or (month == 4 and day <= 19):
return "白羊座"
elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
return "金牛座"
elif (month == 5 and day >= 21) or (month == 6 and day <= 21):
return "双子座"
elif (month == 6 and day >= 22) or (month == 7 and day <= 22):
return "巨蟹座"
elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
return "狮子座"
elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
return "处女座"
elif (month == 9 and day >= 23) or (month == 10 and day <= 23):
return "天秤座"
elif (month == 10 and day >= 24) or (month == 11 and day <= 22):
return "天蝎座"
elif (month == 11 and day >= 23) or (month == 12 and day <= 21):
return "射手座"
elif (month == 12 and day >= 22) or (month == 1 and day <= 19):
return "摩羯座"
elif (month == 1 and day >= 20) or (month == 2 and day <= 18):
return "水瓶座"
elif (month == 2 and day >= 19) or (month == 3 and day <= 20):
return "双鱼座"
else:
return "未知星座"
-
创建标签控件:用于显示星座识别结果的标签,默认文本为空。
-
显示标签:将结果标签显示在窗口上,并设置垂直方向上的内边距为10像素,使其居中显示。
-
创建"确认"按钮:使用
tk.Button()
创建一个按钮控件,文本为"确认",点击按钮时调用send_birth_date()
函数。 -
显示按钮:将"确认"按钮显示在窗口上,并设置垂直方向上的内边距为5像素。
result_label = tk.Label(root, text="", justify=tk.CENTER) result_label.pack(pady=10) btn_confirm = tk.Button(root, text="确认", command=send_birth_date) btn_confirm.pack(pady=5)
- 运行主循环:使用
root.mainloop()
启动Tkinter的主事件循环,让窗口保持
root.mainloop()
完整代码:
import tkinter as tk
import re
from tkinter import messagebox
# 初始化窗口
root = tk.Tk()
root.title("星座识别器")
# 获取屏幕的宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 设置窗口大小,宽度为300像素,高度为200像素
window_width = 300
window_height = 200
# 计算窗口应该放置的坐标位置,使其居中显示
x_position = (screen_width - window_width) // 2
y_position = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")
# 创建标签和输入框
label_birth_date = tk.Label(root, text="出生日期:", justify=tk.CENTER)
label_birth_date.pack(pady=10) # pady是垂直方向上的内边距,用于居中
entry_birth_date = tk.Entry(root)
entry_birth_date.pack()
# 创建“确认”按钮
def send_birth_date():
birth_date = entry_birth_date.get()
# 使用正则表达式检查出生日期是否符合特定格式 xxxx/xx/xx
pattern = r'^\d{4}/\d{2}/\d{2}$'
if re.match(pattern, birth_date):
# 识别星座
constellation = identify_constellation(birth_date)
# 显示结果
result_label.config(text=f"您的星座是:{constellation}")
else:
messagebox.showerror("错误", "请输入正确的出生日期格式(例如:xxxx/xx/xx)")
# 创建函数来进行星座识别
def identify_constellation(birth_date):
# 将日期字符串转换为整数
year, month, day = map(int, birth_date.split('/'))
# 根据出生日期的年份和月份来判断星座
if (month == 3 and day >= 21) or (month == 4 and day <= 19):
return "白羊座"
elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
return "金牛座"
elif (month == 5 and day >= 21) or (month == 6 and day <= 21):
return "双子座"
elif (month == 6 and day >= 22) or (month == 7 and day <= 22):
return "巨蟹座"
elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
return "狮子座"
elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
return "处女座"
elif (month == 9 and day >= 23) or (month == 10 and day <= 23):
return "天秤座"
elif (month == 10 and day >= 24) or (month == 11 and day <= 22):
return "天蝎座"
elif (month == 11 and day >= 23) or (month == 12 and day <= 21):
return "射手座"
elif (month == 12 and day >= 22) or (month == 1 and day <= 19):
return "摩羯座"
elif (month == 1 and day >= 20) or (month == 2 and day <= 18):
return "水瓶座"
elif (month == 2 and day >= 19) or (month == 3 and day <= 20):
return "双鱼座"
else:
return "未知星座"
# 创建用于显示星座的标签
result_label = tk.Label(root, text="", justify=tk.CENTER)
result_label.pack(pady=10)
btn_confirm = tk.Button(root, text="确认", command=send_birth_date)
btn_confirm.pack(pady=5)
# 运行主循环
root.mainloop()