简单说说,Qt星座计算

鱼弦
全栈领域优质创作者
博客专家认证
2023-08-14 11:35:19

效果图:

 

要使用 PyQt 获取当前日期和计算星座,你可以使用 Python 的 datetime 模块来获取当前日期,然后根据日期计算星座。以下是一个使用 PyQt 的示例代码:

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
from PyQt5.QtCore import QDateTime, Qt


def get_constellation(date):
    # 根据日期计算星座
    month = date.month
    day = date.day

    if (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 "双鱼座"
    elif (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 "射手座"
    else:
        return "摩羯座"


app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("星座计算")
window.setGeometry(100, 100, 300, 100)

current_date = QDateTime.currentDateTime().date()
current_constellation = get_constellation(current_date)

label = QLabel(window)
label.setAlignment(Qt.AlignCenter)
label.setText(f"今天是 {current_date.toString('yyyy-MM-dd')},你的星座是 {current_constellation}")

window.show()
sys.exit(app.exec_())

这个示例创建了一个简单的 PyQt 窗口,显示当前日期和计算得到的星座。你可以根据需要进行修改和美化窗口的外观。运行代码后,将会打开一个窗口显示当前日期和对应的星座信息。

...全文
372 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

125

社区成员

发帖
与我相关
我的任务
社区描述
全栈技术社区,指在创建一个和谐的技术群体,共同学习进步
qt前端tcp/ip 个人社区 四川省·成都市
社区管理员
  • 鱼弦
  • M malloc
  • 恰柠
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧