pyqt项目19-QCalendarWidget(日历)

  • Post author:
  • Post category:其他


提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

@

TOC




前言


提示:这里可以添加本文要记录的大概内容:



提示:以下是本篇文章正文内容,下面案例可供参考



一、所学内容

1.1 QCalendarWidget

1.1.1 selectionChanged

1.2 QPalette(设置字体颜色)

pe = QPalette()
pe.setColor(QPalette.WindowText, Qt.red)
self.label_3.setPalette(pe)



二、完整代码

2.1 界面逻辑

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.13.1
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(522, 441)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
        self.calendarWidget.setGeometry(QtCore.QRect(10, 0, 501, 291))
        self.calendarWidget.setObjectName("calendarWidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 310, 501, 16))
        self.label.setText("")
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(10, 330, 501, 16))
        self.label_2.setText("")
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(10, 350, 501, 16))
        self.label_3.setText("")
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(10, 370, 501, 16))
        self.label_4.setText("")
        self.label_4.setObjectName("label_4")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 522, 19))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.calendarWidget.selectionChanged.connect(MainWindow.change_value)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

2.2 业务逻辑

import random
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from untitled import Ui_MainWindow
from PyQt5.QtCore import Qt, QBasicTimer
import urllib
import urllib.request as request
import urllib.error as error
import json
from PyQt5.QtGui import QPalette
from PyQt5.QtCore import Qt

# ======haozh-検索画面(这里继承的类就是Qtdesigner生成出来的.py文件)=====
class My_Form(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(My_Form, self).__init__()
        self.setupUi(self)
        # 设置字体颜色
        pe = QPalette()
        pe.setColor(QPalette.WindowText, Qt.red)
        self.label_3.setPalette(pe)
        pe.setColor(QPalette.WindowText, Qt.green)
        self.label_4.setPalette(pe)

    def change_value(self):
        date = self.calendarWidget.selectedDate().toString("yyyy-MM-dd dddd")
        self.request1(self.appkey, date)

    def request1(self, appkey, date):
        api_url = 'http://v.juhe.cn/calendar/day'
        str_date =[str(int(d)) for d in date[:10].split('-')]
        params_dict = {
            "key": "d5542be7b7719c93b74344307aecede6",  # 查询天气的城市名称,如:北京、苏州、上海
            "date": str_date[0] + '-' + str_date[1] + '-' + str_date[2],  # 您申请的接口API接口请求Key
        }
        params = urllib.parse.urlencode(params_dict)
        req = request.Request(api_url, params.encode())
        req = request.Request(api_url, params.encode())
        response = request.urlopen(req)
        result = response.read().decode('utf-8')
        res = json.loads(result)
        print(res)
        if res:
            error_code = res["error_code"]
            data = res["result"]['data']
            if error_code == 0:  # 成功请求
                self.label.setText("阳历:" + date)
                self.label_2.setText("阴历:" + data["lunarYear"] + data["lunar"])
                self.label_3.setText("忌:" + data["avoid"])
                self.label_4.setText("宜:" + data["suit"])
            else:
                QMessageBox.Warning(self, "警告", "错误代码:" + str(res["error_code"]) + "错误原因:" + res["reason"])
        else:
            QMessageBox.Warning(self, "警告", "API请求失败")

# ======haozh-主程序调用=====
if __name__=='__main__':
    app=QApplication(sys.argv)
    index=My_Form()
    index.show()
    sys.exit(app.exec_())



总结

提示:这里对文章进行总结:

例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。



版权声明:本文为weixin_45536181原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。