江湖救急pyqt5的一个小程序

 

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
import requests
import time


class Ui_huiLv_Form(object):
    def setupUi(self, huiLv_Form):
        huiLv_Form.setObjectName("huiLv_Form")
        huiLv_Form.resize(630, 300)
        self.yuanS_comboBox = QtWidgets.QComboBox(huiLv_Form)
        self.yuanS_comboBox.setGeometry(QtCore.QRect(226, 150, 69, 31))
        self.yuanS_comboBox.setStyleSheet("font: 75 12pt \"微软雅黑\";")
        self.yuanS_comboBox.setObjectName("yuanS_comboBox")
        self.label = QtWidgets.QLabel(huiLv_Form)
        self.label.setGeometry(QtCore.QRect(300, 150, 21, 30))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
        self.label.setSizePolicy(sizePolicy)
        self.label.setSizeIncrement(QtCore.QSize(0, 0))
        self.label.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(29)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.duiH_comboBox = QtWidgets.QComboBox(huiLv_Form)
        self.duiH_comboBox.setGeometry(QtCore.QRect(326, 150, 69, 31))
        self.duiH_comboBox.setStyleSheet("font: 75 12pt \"微软雅黑\";")
        self.duiH_comboBox.setObjectName("duiH_comboBox")
        self.zhuanH_pushButton = QtWidgets.QPushButton(huiLv_Form)
        self.zhuanH_pushButton.setGeometry(QtCore.QRect(418, 150, 75, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(10)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.zhuanH_pushButton.setFont(font)
        self.zhuanH_pushButton.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.zhuanH_pushButton.setMouseTracking(False)
        self.zhuanH_pushButton.setTabletTracking(False)
        self.zhuanH_pushButton.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.zhuanH_pushButton.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        self.zhuanH_pushButton.setStyleSheet("color: rgb(255, 170, 0)")
        self.zhuanH_pushButton.setAutoExclusive(False)
        self.zhuanH_pushButton.setObjectName("zhuanH_pushButton")
        self.textBrowser = QtWidgets.QTextBrowser(huiLv_Form)
        self.textBrowser.setGeometry(QtCore.QRect(120, 90, 371, 41))
        self.textBrowser.setStyleSheet("font: 75 14pt \"微软雅黑\";")
        self.textBrowser.setObjectName("textBrowser")
        self.jinE_doubleSpinBox = QtWidgets.QDoubleSpinBox(huiLv_Form)
        self.jinE_doubleSpinBox.setGeometry(QtCore.QRect(118, 150, 101, 31))
        self.jinE_doubleSpinBox.setMinimumSize(QtCore.QSize(101, 31))
        self.jinE_doubleSpinBox.setStyleSheet("font: 12pt \"微软雅黑\";")
        self.jinE_doubleSpinBox.setMaximum(9999999999.99)
        self.jinE_doubleSpinBox.setProperty("value", 1.0)
        self.jinE_doubleSpinBox.setObjectName("jinE_doubleSpinBox")
        self.time_label = QtWidgets.QLabel(huiLv_Form)
        self.time_label.setGeometry(QtCore.QRect(400, 240, 200, 30))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.time_label.sizePolicy().hasHeightForWidth())
        self.time_label.setSizePolicy(sizePolicy)
        self.time_label.setSizeIncrement(QtCore.QSize(0, 0))
        self.time_label.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(False)
        font.setWeight(300)
        self.time_label.setFont(font)
        self.time_label.setText("")
        self.time_label.setObjectName("time_label")

        self.retranslateUi(huiLv_Form)
        QtCore.QMetaObject.connectSlotsByName(huiLv_Form)
        huiLv_Form.setTabOrder(self.jinE_doubleSpinBox, self.yuanS_comboBox)
        huiLv_Form.setTabOrder(self.yuanS_comboBox, self.duiH_comboBox)
        huiLv_Form.setTabOrder(self.duiH_comboBox, self.zhuanH_pushButton)
        huiLv_Form.setTabOrder(self.zhuanH_pushButton, self.textBrowser)

    def retranslateUi(self, huiLv_Form):
        _translate = QtCore.QCoreApplication.translate
        huiLv_Form.setWindowTitle(_translate("huiLv_Form", "汇率计算器"))
        huiLv_Form.setWhatsThis(_translate("huiLv_Form", "<html><head/><body><p><br/></p></body></html>"))
        self.label.setText(_translate("huiLv_Form", "="))
        self.zhuanH_pushButton.setText(_translate("huiLv_Form", "转换"))


class MyMainForm(QMainWindow, Ui_huiLv_Form):
    def __init__(self, parent=None):
        super().__init__(parent)  # 调用父类构造函数,创建QMainWindow窗体
        self.setupUi(self)
        self.yuanS_comboBox.addItems(list(rate.keys()))
        self.duiH_comboBox.addItems(list(rate.keys()))
        self.time_label.setText('汇率更新时间:' + update_time)  # 设置time_Label 内容为当前时间
        self.textBrowser.setText('点击转换获货币兑换关系')  # 设置textBrowser 内容为“xxx关系”
        self.zhuanH_pushButton.clicked.connect(self.rateChange)  # 按钮点击信号关联槽函数rateChange

    def rateChange(self):
        n = self.jinE_doubleSpinBox.value()
        x = self.yuanS_comboBox.currentText()
        y = self.duiH_comboBox.currentText()

        result = round(rate[y] / rate[x] * n, 4)
        str_print = str(n) + '   ' + x + '  =  ' + str(result) + '   ' + y
        self.textBrowser.setText(str_print)


def Get_huilv():
    # 获取汇率数据
    url = "https://api.exchangerate-api.com/v4/latest/CNY"
    redata = requests.get(url)

    # 获取汇率关系字段
    dic_ = eval(redata.text)
    rate = dic_['rates']

    # 获取汇率更新时间
    update_time = time.localtime(dic_['time_last_updated'])
    update_time = time.strftime("%Y-%m-%d %H:%M:%S", update_time)

    return rate, update_time


if __name__ == "__main__":
    rate, update_time = Get_huilv()
    # 固定的,PyQt5程序都需要QApplication对象
    # sys.argv是命令行参数列表,确保程序可以双击运行
    app = QApplication(sys.argv)
    # 初始化
    myWin = MyMainForm()
    # 将窗口控件显示在屏幕上
    myWin.show()
    # 程序运行,sys.exit方法确保程序完整退出
    sys.exit(app.exec_())

如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632