125
社区成员




效果图:
要使用 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 窗口,显示当前日期和计算得到的星座。你可以根据需要进行修改和美化窗口的外观。运行代码后,将会打开一个窗口显示当前日期和对应的星座信息。