├── README.md
├── 多窗口项目模板
├── Algorithms.py
├── Login_Dialog.py
├── Login_Dialog.ui
├── Main_Window.py
├── Main_Window.ui
├── __pycache__
│ ├── Algorithms.cpython-39.pyc
│ ├── Login_Dialog.cpython-39.pyc
│ ├── Main_Window.cpython-39.pyc
│ └── Template.cpython-39.pyc
└── main.py
├── 天气查询
├── Weather.py
├── Weather.ui
├── __pycache__
│ └── Weather.cpython-39.pyc
├── run.py
└── 天气.PNG
├── 实时汇率换算
├── Exchange.py
├── Exchange.ui
├── run.py
└── 汇率转换.PNG
└── 项目模板
├── Template.py
├── Template.ui
├── __pycache__
└── Template.cpython-39.pyc
└── main.py
/README.md:
--------------------------------------------------------------------------------
1 | # PyQt5项目实战
2 |
3 | ## 主窗口项目模板
4 |
5 | 本项目提供PyQt5编程通用的项目工程模板,用于将界面与逻辑分离。其使用方法详见[main.py](https://github.com/wzy6642/PyQt5/blob/master/%E9%A1%B9%E7%9B%AE%E6%A8%A1%E6%9D%BF/main.py)中的注释部分。
6 |
7 | ## 多窗口项目模板
8 | 本项目提供一个代码模板,用于解决窗口跳转的需求,并将界面、逻辑、算法进行分离。其使用方法详见[main.py](https://github.com/wzy6642/PyQt5/blob/master/%E5%A4%9A%E7%AA%97%E5%8F%A3%E9%A1%B9%E7%9B%AE%E6%A8%A1%E6%9D%BF/main.py)中的注释部分。
9 |
10 |
11 | ## 汇率计算器
12 |
13 | 本项目的主要功能是实时获取每天的汇率,并提供一套完整的计算工具完成不同币种之间的汇率转换。项目代码详见[code](https://github.com/wzy6642/PyQt5/blob/master/%E5%AE%9E%E6%97%B6%E6%B1%87%E7%8E%87%E6%8D%A2%E7%AE%97/run.py),具体效果为:
14 |
15 |

16 |
17 |
18 | ## 天气查询
19 |
20 | 本项目借助百度正逆地理编码API以及天气状况查询API实现输入地名,获取其经纬度以及当前天气状况,并将未来一段时间的温度等信息以曲线图的形式进行展示。项目代码详见[code](https://github.com/wzy6642/PyQt5/blob/master/%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2/run.py),具体效果为:
21 |
22 |

23 |
24 |
--------------------------------------------------------------------------------
/多窗口项目模板/Algorithms.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # @Time : 2021/8/12 14:06
3 | # @Author : Zhenyu Wu
4 | # @Email : wuzhenyuai@gmail.com
5 | # @File : Algorithms.py
6 | # @Software: PyCharm
7 | # 本软件对应的算法部分
8 |
9 |
10 | class algorithm_name:
11 | # 初始化部分
12 | def __init__(self, value_1):
13 | self.value_1 = value_1
14 |
15 | # 具体计算方法
16 | def calculate(self):
17 | print("测试算法")
18 |
--------------------------------------------------------------------------------
/多窗口项目模板/Login_Dialog.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # Form implementation generated from reading ui file 'Login_Dialog.ui'
4 | #
5 | # Created by: PyQt5 UI code generator 5.15.4
6 | #
7 | # WARNING: Any manual changes made to this file will be lost when pyuic5 is
8 | # run again. Do not edit this file unless you know what you are doing.
9 |
10 |
11 | from PyQt5 import QtCore, QtGui, QtWidgets
12 |
13 |
14 | class Ui_Dialog(object):
15 | def setupUi(self, Dialog):
16 | Dialog.setObjectName("Dialog")
17 | Dialog.resize(339, 140)
18 | self.dialog_button = QtWidgets.QPushButton(Dialog)
19 | self.dialog_button.setGeometry(QtCore.QRect(120, 50, 93, 28))
20 | self.dialog_button.setObjectName("dialog_button")
21 |
22 | self.retranslateUi(Dialog)
23 | QtCore.QMetaObject.connectSlotsByName(Dialog)
24 |
25 | def retranslateUi(self, Dialog):
26 | _translate = QtCore.QCoreApplication.translate
27 | Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
28 | self.dialog_button.setText(_translate("Dialog", "测试按键"))
29 |
--------------------------------------------------------------------------------
/多窗口项目模板/Login_Dialog.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Dialog
4 |
5 |
6 |
7 | 0
8 | 0
9 | 339
10 | 140
11 |
12 |
13 |
14 | Dialog
15 |
16 |
17 |
18 |
19 | 120
20 | 50
21 | 93
22 | 28
23 |
24 |
25 |
26 | 测试按键
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/多窗口项目模板/Main_Window.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # Form implementation generated from reading ui file 'Main_Window.ui'
4 | #
5 | # Created by: PyQt5 UI code generator 5.15.4
6 | #
7 | # WARNING: Any manual changes made to this file will be lost when pyuic5 is
8 | # run again. Do not edit this file unless you know what you are doing.
9 |
10 |
11 | from PyQt5 import QtCore, QtGui, QtWidgets
12 |
13 |
14 | class Ui_MainWindow(object):
15 | def setupUi(self, MainWindow):
16 | MainWindow.setObjectName("MainWindow")
17 | MainWindow.resize(306, 140)
18 | self.centralwidget = QtWidgets.QWidget(MainWindow)
19 | self.centralwidget.setObjectName("centralwidget")
20 | self.test_button = QtWidgets.QPushButton(self.centralwidget)
21 | self.test_button.setGeometry(QtCore.QRect(100, 50, 93, 28))
22 | self.test_button.setObjectName("test_button")
23 | MainWindow.setCentralWidget(self.centralwidget)
24 | self.menubar = QtWidgets.QMenuBar(MainWindow)
25 | self.menubar.setGeometry(QtCore.QRect(0, 0, 306, 26))
26 | self.menubar.setObjectName("menubar")
27 | MainWindow.setMenuBar(self.menubar)
28 | self.statusbar = QtWidgets.QStatusBar(MainWindow)
29 | self.statusbar.setObjectName("statusbar")
30 | MainWindow.setStatusBar(self.statusbar)
31 |
32 | self.retranslateUi(MainWindow)
33 | QtCore.QMetaObject.connectSlotsByName(MainWindow)
34 |
35 | def retranslateUi(self, MainWindow):
36 | _translate = QtCore.QCoreApplication.translate
37 | MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
38 | self.test_button.setText(_translate("MainWindow", "测试按键"))
39 |
--------------------------------------------------------------------------------
/多窗口项目模板/Main_Window.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 306
10 | 140
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
18 |
19 |
20 | 100
21 | 50
22 | 93
23 | 28
24 |
25 |
26 |
27 | 测试按键
28 |
29 |
30 |
31 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/多窗口项目模板/__pycache__/Algorithms.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzy6642/PyQt5/81f53a2e7fbae617290531139533142e481c776e/多窗口项目模板/__pycache__/Algorithms.cpython-39.pyc
--------------------------------------------------------------------------------
/多窗口项目模板/__pycache__/Login_Dialog.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzy6642/PyQt5/81f53a2e7fbae617290531139533142e481c776e/多窗口项目模板/__pycache__/Login_Dialog.cpython-39.pyc
--------------------------------------------------------------------------------
/多窗口项目模板/__pycache__/Main_Window.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzy6642/PyQt5/81f53a2e7fbae617290531139533142e481c776e/多窗口项目模板/__pycache__/Main_Window.cpython-39.pyc
--------------------------------------------------------------------------------
/多窗口项目模板/__pycache__/Template.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzy6642/PyQt5/81f53a2e7fbae617290531139533142e481c776e/多窗口项目模板/__pycache__/Template.cpython-39.pyc
--------------------------------------------------------------------------------
/多窗口项目模板/main.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # @Time : 2021/8/11 9:12
3 | # @Author : Zhenyu Wu
4 | # @Email : wuzhenyuai@gmail.com
5 | # @File : main.py
6 | # @Software: PyCharm
7 | # 顺序:
8 | # 1、Tools -> External Tools -> QtDesigner(绘制界面) -> 保存为`UI文件名.ui`
9 | # 2、右击`UI文件名.ui` -> External Tools -> PyUIC -> 生成`UI文件名.py`
10 | # 3、右击项目文件夹 -> Mark Directory as -> Sources Root
11 | # 4、新建`main.py` -> 添加下述代码并运行
12 | # 界面与逻辑分离
13 | # 后续将日志功能加入
14 | import sys
15 |
16 | from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
17 |
18 | from Main_Window import Ui_MainWindow # 导入主窗口
19 | from Login_Dialog import Ui_Dialog # 导入弹窗
20 | from Algorithms import algorithm_name # 导入要执行的算法
21 |
22 |
23 | # 新建类继承于UI类,方便进一步书写逻辑
24 | class Application(QMainWindow, Ui_MainWindow):
25 | def __init__(self, parent=None):
26 | super(Application, self).__init__(parent)
27 | self.setupUi(self) # 创建界面
28 | ## 一些控件的属性初始化代码
29 | self.window_attribute()
30 |
31 | ## 一些算法实例化代码
32 | self.algorithm_name = algorithm_name(value_1=None)
33 |
34 | ## 信号和槽函数部分
35 | self.test_button.clicked.connect(lambda: self.test(
36 | self.test_button,
37 | ))
38 |
39 | ## 控件属性定义部分
40 | def window_attribute(self):
41 | # 属性的具体定义部分
42 | self.setWindowTitle("PyQt5主窗口模板")
43 |
44 | ## 自定义槽函数部分
45 | def test(self, button):
46 | # 这里编写触发事件对应的执行代码
47 | print(button.text())
48 | self.algorithm_name.calculate()
49 |
50 |
51 | # 新建类继承于对话框类,方便进一步书写逻辑
52 | class Login(QDialog, Ui_Dialog):
53 | def __init__(self, parent=None):
54 | super(Login, self).__init__(parent)
55 | self.setupUi(self) # 创建界面
56 | ## 一些控件的属性初始化代码
57 | self.dialog_attribute()
58 |
59 | ## 一些算法实例化代码
60 | self.algorithm_name = algorithm_name(value_1=None)
61 |
62 | ## 信号和槽函数部分
63 | self.dialog_button.clicked.connect(lambda: self.test(
64 | self.dialog_button,
65 | ))
66 |
67 | ## 控件属性定义部分
68 | def dialog_attribute(self):
69 | # 属性的具体定义部分
70 | self.setWindowTitle("登录窗口")
71 |
72 | ## 自定义槽函数部分
73 | def test(self, button):
74 | # 这里编写触发事件对应的执行代码
75 | self.algorithm_name.calculate()
76 | self.close()
77 |
78 |
79 | # 设计不同页面之间的逻辑关系
80 | def Interaction_Logic(main_window, login_dialog):
81 | main_window.test_button.clicked.connect(login_dialog.show)
82 |
83 |
84 | if __name__ == '__main__':
85 | app = QApplication(sys.argv) # 创建应用程序对象
86 | mainWindow = Application() # 实例化界面
87 | loginDialog = Login() # 实例化对话框窗口
88 | Interaction_Logic(mainWindow, loginDialog) # 页面之间的逻辑设计
89 | mainWindow.show() # 窗口显示
90 | sys.exit(app.exec_()) # 主循环结束
91 |
--------------------------------------------------------------------------------
/天气查询/Weather.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # Form implementation generated from reading ui file 'Weather.ui'
4 | #
5 | # Created by: PyQt5 UI code generator 5.15.4
6 | #
7 | # WARNING: Any manual changes made to this file will be lost when pyuic5 is
8 | # run again. Do not edit this file unless you know what you are doing.
9 |
10 |
11 | from PyQt5 import QtCore, QtGui, QtWidgets
12 |
13 |
14 | class Ui_Form(object):
15 | def setupUi(self, Form):
16 | Form.setObjectName("Form")
17 | Form.resize(1057, 811)
18 | self.groupBox = QtWidgets.QGroupBox(Form)
19 | self.groupBox.setGeometry(QtCore.QRect(20, 20, 361, 231))
20 | font = QtGui.QFont()
21 | font.setFamily("等线")
22 | font.setPointSize(13)
23 | self.groupBox.setFont(font)
24 | self.groupBox.setObjectName("groupBox")
25 | self.widget = QtWidgets.QWidget(self.groupBox)
26 | self.widget.setGeometry(QtCore.QRect(10, 30, 341, 190))
27 | self.widget.setObjectName("widget")
28 | self.formLayout = QtWidgets.QFormLayout(self.widget)
29 | self.formLayout.setContentsMargins(0, 0, 0, 0)
30 | self.formLayout.setObjectName("formLayout")
31 | self.location_label = QtWidgets.QLabel(self.widget)
32 | font = QtGui.QFont()
33 | font.setFamily("等线")
34 | font.setPointSize(13)
35 | self.location_label.setFont(font)
36 | self.location_label.setObjectName("location_label")
37 | self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.location_label)
38 | self.position = QtWidgets.QLineEdit(self.widget)
39 | self.position.setObjectName("position")
40 | self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.position)
41 | self.longitude_label = QtWidgets.QLabel(self.widget)
42 | font = QtGui.QFont()
43 | font.setFamily("等线")
44 | font.setPointSize(13)
45 | self.longitude_label.setFont(font)
46 | self.longitude_label.setObjectName("longitude_label")
47 | self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.longitude_label)
48 | self.longitude = QtWidgets.QLineEdit(self.widget)
49 | self.longitude.setReadOnly(True)
50 | self.longitude.setObjectName("longitude")
51 | self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.longitude)
52 | self.latitude_label = QtWidgets.QLabel(self.widget)
53 | font = QtGui.QFont()
54 | font.setFamily("等线")
55 | font.setPointSize(13)
56 | self.latitude_label.setFont(font)
57 | self.latitude_label.setObjectName("latitude_label")
58 | self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.latitude_label)
59 | self.latitude = QtWidgets.QLineEdit(self.widget)
60 | self.latitude.setReadOnly(True)
61 | self.latitude.setObjectName("latitude")
62 | self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.latitude)
63 | self.information_label = QtWidgets.QLabel(self.widget)
64 | font = QtGui.QFont()
65 | font.setFamily("等线")
66 | font.setPointSize(13)
67 | self.information_label.setFont(font)
68 | self.information_label.setObjectName("information_label")
69 | self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.information_label)
70 | self.information = QtWidgets.QLineEdit(self.widget)
71 | self.information.setReadOnly(True)
72 | self.information.setObjectName("information")
73 | self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.information)
74 | self.loc_scan = QtWidgets.QPushButton(self.widget)
75 | font = QtGui.QFont()
76 | font.setFamily("等线")
77 | font.setPointSize(12)
78 | self.loc_scan.setFont(font)
79 | self.loc_scan.setObjectName("loc_scan")
80 | self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.loc_scan)
81 | self.groupBox_2 = QtWidgets.QGroupBox(Form)
82 | self.groupBox_2.setGeometry(QtCore.QRect(400, 20, 361, 231))
83 | font = QtGui.QFont()
84 | font.setFamily("等线")
85 | font.setPointSize(13)
86 | self.groupBox_2.setFont(font)
87 | self.groupBox_2.setObjectName("groupBox_2")
88 | self.widget1 = QtWidgets.QWidget(self.groupBox_2)
89 | self.widget1.setGeometry(QtCore.QRect(10, 30, 341, 191))
90 | self.widget1.setObjectName("widget1")
91 | self.formLayout_2 = QtWidgets.QFormLayout(self.widget1)
92 | self.formLayout_2.setContentsMargins(0, 0, 0, 0)
93 | self.formLayout_2.setObjectName("formLayout_2")
94 | self.temp_label = QtWidgets.QLabel(self.widget1)
95 | font = QtGui.QFont()
96 | font.setFamily("等线")
97 | font.setPointSize(13)
98 | self.temp_label.setFont(font)
99 | self.temp_label.setObjectName("temp_label")
100 | self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.temp_label)
101 | self.temp_location = QtWidgets.QLineEdit(self.widget1)
102 | self.temp_location.setReadOnly(True)
103 | self.temp_location.setObjectName("temp_location")
104 | self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.temp_location)
105 | self.time_label = QtWidgets.QLabel(self.widget1)
106 | font = QtGui.QFont()
107 | font.setFamily("等线")
108 | font.setPointSize(13)
109 | self.time_label.setFont(font)
110 | self.time_label.setObjectName("time_label")
111 | self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.time_label)
112 | self.time_stamp = QtWidgets.QLineEdit(self.widget1)
113 | self.time_stamp.setReadOnly(True)
114 | self.time_stamp.setObjectName("time_stamp")
115 | self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.time_stamp)
116 | self.label = QtWidgets.QLabel(self.widget1)
117 | font = QtGui.QFont()
118 | font.setFamily("等线")
119 | font.setPointSize(13)
120 | self.label.setFont(font)
121 | self.label.setObjectName("label")
122 | self.formLayout_2.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label)
123 | self.temp_now = QtWidgets.QLineEdit(self.widget1)
124 | self.temp_now.setReadOnly(True)
125 | self.temp_now.setObjectName("temp_now")
126 | self.formLayout_2.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.temp_now)
127 | self.humidity_label = QtWidgets.QLabel(self.widget1)
128 | font = QtGui.QFont()
129 | font.setFamily("等线")
130 | font.setPointSize(13)
131 | self.humidity_label.setFont(font)
132 | self.humidity_label.setObjectName("humidity_label")
133 | self.formLayout_2.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.humidity_label)
134 | self.humidity_now = QtWidgets.QLineEdit(self.widget1)
135 | self.humidity_now.setReadOnly(True)
136 | self.humidity_now.setObjectName("humidity_now")
137 | self.formLayout_2.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.humidity_now)
138 | self.pressure_label = QtWidgets.QLabel(self.widget1)
139 | font = QtGui.QFont()
140 | font.setFamily("等线")
141 | font.setPointSize(13)
142 | self.pressure_label.setFont(font)
143 | self.pressure_label.setObjectName("pressure_label")
144 | self.formLayout_2.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.pressure_label)
145 | self.pressure = QtWidgets.QLineEdit(self.widget1)
146 | self.pressure.setReadOnly(True)
147 | self.pressure.setObjectName("pressure")
148 | self.formLayout_2.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.pressure)
149 | self.groupBox_3 = QtWidgets.QGroupBox(Form)
150 | self.groupBox_3.setGeometry(QtCore.QRect(780, 20, 241, 231))
151 | font = QtGui.QFont()
152 | font.setFamily("等线")
153 | font.setPointSize(13)
154 | self.groupBox_3.setFont(font)
155 | self.groupBox_3.setObjectName("groupBox_3")
156 | self.widget2 = QtWidgets.QWidget(self.groupBox_3)
157 | self.widget2.setGeometry(QtCore.QRect(30, 30, 181, 181))
158 | self.widget2.setObjectName("widget2")
159 | self.verticalLayout = QtWidgets.QVBoxLayout(self.widget2)
160 | self.verticalLayout.setContentsMargins(0, 0, 0, 0)
161 | self.verticalLayout.setObjectName("verticalLayout")
162 | self.temp_radioButton = QtWidgets.QRadioButton(self.widget2)
163 | font = QtGui.QFont()
164 | font.setFamily("等线")
165 | font.setPointSize(13)
166 | self.temp_radioButton.setFont(font)
167 | self.temp_radioButton.setObjectName("temp_radioButton")
168 | self.verticalLayout.addWidget(self.temp_radioButton)
169 | self.radioButton = QtWidgets.QRadioButton(self.widget2)
170 | font = QtGui.QFont()
171 | font.setFamily("等线")
172 | font.setPointSize(13)
173 | self.radioButton.setFont(font)
174 | self.radioButton.setObjectName("radioButton")
175 | self.verticalLayout.addWidget(self.radioButton)
176 | self.radioButton_2 = QtWidgets.QRadioButton(self.widget2)
177 | font = QtGui.QFont()
178 | font.setFamily("等线")
179 | font.setPointSize(13)
180 | self.radioButton_2.setFont(font)
181 | self.radioButton_2.setObjectName("radioButton_2")
182 | self.verticalLayout.addWidget(self.radioButton_2)
183 | self.radioButton_3 = QtWidgets.QRadioButton(self.widget2)
184 | font = QtGui.QFont()
185 | font.setFamily("等线")
186 | font.setPointSize(13)
187 | self.radioButton_3.setFont(font)
188 | self.radioButton_3.setObjectName("radioButton_3")
189 | self.verticalLayout.addWidget(self.radioButton_3)
190 | self.radioButton_4 = QtWidgets.QRadioButton(self.widget2)
191 | font = QtGui.QFont()
192 | font.setFamily("等线")
193 | font.setPointSize(13)
194 | self.radioButton_4.setFont(font)
195 | self.radioButton_4.setObjectName("radioButton_4")
196 | self.verticalLayout.addWidget(self.radioButton_4)
197 | self.img = QtWidgets.QLabel(Form)
198 | self.img.setGeometry(QtCore.QRect(20, 270, 1001, 501))
199 | self.img.setText("")
200 | self.img.setObjectName("img")
201 | self.location_label.setBuddy(self.position)
202 | self.longitude_label.setBuddy(self.longitude)
203 | self.latitude_label.setBuddy(self.latitude)
204 |
205 | self.retranslateUi(Form)
206 | QtCore.QMetaObject.connectSlotsByName(Form)
207 |
208 | def retranslateUi(self, Form):
209 | _translate = QtCore.QCoreApplication.translate
210 | Form.setWindowTitle(_translate("Form", "Form"))
211 | self.groupBox.setTitle(_translate("Form", "位置信息"))
212 | self.location_label.setText(_translate("Form", "地理位置(&A): "))
213 | self.longitude_label.setText(_translate("Form", "经度: "))
214 | self.latitude_label.setText(_translate("Form", "纬度: "))
215 | self.information_label.setText(_translate("Form", "详细信息: "))
216 | self.loc_scan.setText(_translate("Form", "查询经纬度"))
217 | self.groupBox_2.setTitle(_translate("Form", "天气信息"))
218 | self.temp_label.setText(_translate("Form", "温度采样地址: "))
219 | self.time_label.setText(_translate("Form", "更新时间: "))
220 | self.label.setText(_translate("Form", "当前温度: "))
221 | self.humidity_label.setText(_translate("Form", "当前湿度: "))
222 | self.pressure_label.setText(_translate("Form", "当前气压: "))
223 | self.groupBox_3.setTitle(_translate("Form", "可视化图框"))
224 | self.temp_radioButton.setText(_translate("Form", "未来温度"))
225 | self.radioButton.setText(_translate("Form", "未来风速"))
226 | self.radioButton_2.setText(_translate("Form", "未来天气"))
227 | self.radioButton_3.setText(_translate("Form", "日出/日落时间"))
228 | self.radioButton_4.setText(_translate("Form", "最高/最低温度"))
229 |
--------------------------------------------------------------------------------
/天气查询/Weather.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 1057
10 | 811
11 |
12 |
13 |
14 | Form
15 |
16 |
17 |
18 |
19 | 20
20 | 20
21 | 361
22 | 231
23 |
24 |
25 |
26 |
27 | 等线
28 | 13
29 |
30 |
31 |
32 | 位置信息
33 |
34 |
35 |
36 |
37 | 10
38 | 30
39 | 341
40 | 190
41 |
42 |
43 |
44 | -
45 |
46 |
47 |
48 | 等线
49 | 13
50 |
51 |
52 |
53 | 地理位置(&A):
54 |
55 |
56 | position
57 |
58 |
59 |
60 | -
61 |
62 |
63 | -
64 |
65 |
66 |
67 | 等线
68 | 13
69 |
70 |
71 |
72 | 经度:
73 |
74 |
75 | longitude
76 |
77 |
78 |
79 | -
80 |
81 |
82 | true
83 |
84 |
85 |
86 | -
87 |
88 |
89 |
90 | 等线
91 | 13
92 |
93 |
94 |
95 | 纬度:
96 |
97 |
98 | latitude
99 |
100 |
101 |
102 | -
103 |
104 |
105 | true
106 |
107 |
108 |
109 | -
110 |
111 |
112 |
113 | 等线
114 | 13
115 |
116 |
117 |
118 | 详细信息:
119 |
120 |
121 |
122 | -
123 |
124 |
125 | true
126 |
127 |
128 |
129 | -
130 |
131 |
132 |
133 | 等线
134 | 12
135 |
136 |
137 |
138 | 查询经纬度
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 | 400
149 | 20
150 | 361
151 | 231
152 |
153 |
154 |
155 |
156 | 等线
157 | 13
158 |
159 |
160 |
161 | 天气信息
162 |
163 |
164 |
165 |
166 | 10
167 | 30
168 | 341
169 | 191
170 |
171 |
172 |
173 | -
174 |
175 |
176 |
177 | 等线
178 | 13
179 |
180 |
181 |
182 | 温度采样地址:
183 |
184 |
185 |
186 | -
187 |
188 |
189 | true
190 |
191 |
192 |
193 | -
194 |
195 |
196 |
197 | 等线
198 | 13
199 |
200 |
201 |
202 | 更新时间:
203 |
204 |
205 |
206 | -
207 |
208 |
209 | true
210 |
211 |
212 |
213 | -
214 |
215 |
216 |
217 | 等线
218 | 13
219 |
220 |
221 |
222 | 当前温度:
223 |
224 |
225 |
226 | -
227 |
228 |
229 | true
230 |
231 |
232 |
233 | -
234 |
235 |
236 |
237 | 等线
238 | 13
239 |
240 |
241 |
242 | 当前湿度:
243 |
244 |
245 |
246 | -
247 |
248 |
249 | true
250 |
251 |
252 |
253 | -
254 |
255 |
256 |
257 | 等线
258 | 13
259 |
260 |
261 |
262 | 当前气压:
263 |
264 |
265 |
266 | -
267 |
268 |
269 | true
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 | 780
280 | 20
281 | 241
282 | 231
283 |
284 |
285 |
286 |
287 | 等线
288 | 13
289 |
290 |
291 |
292 | 可视化图框
293 |
294 |
295 |
296 |
297 | 30
298 | 30
299 | 181
300 | 181
301 |
302 |
303 |
304 | -
305 |
306 |
307 |
308 | 等线
309 | 13
310 |
311 |
312 |
313 | 未来温度
314 |
315 |
316 |
317 | -
318 |
319 |
320 |
321 | 等线
322 | 13
323 |
324 |
325 |
326 | 未来风速
327 |
328 |
329 |
330 | -
331 |
332 |
333 |
334 | 等线
335 | 13
336 |
337 |
338 |
339 | 未来天气
340 |
341 |
342 |
343 | -
344 |
345 |
346 |
347 | 等线
348 | 13
349 |
350 |
351 |
352 | 日出/日落时间
353 |
354 |
355 |
356 | -
357 |
358 |
359 |
360 | 等线
361 | 13
362 |
363 |
364 |
365 | 最高/最低温度
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 | 20
376 | 270
377 | 1001
378 | 501
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
--------------------------------------------------------------------------------
/天气查询/__pycache__/Weather.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzy6642/PyQt5/81f53a2e7fbae617290531139533142e481c776e/天气查询/__pycache__/Weather.cpython-39.pyc
--------------------------------------------------------------------------------
/天气查询/run.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # @Time : 2021/8/10 15:39
3 | # @Author : Zhenyu Wu
4 | # @Email : wuzhenyuai@gmail.com
5 | # @File : run.py
6 | # @Software: PyCharm
7 | # @Reference: https://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
8 | # https://res.abeim.cn/api/weather/doc.php
9 | import sys
10 | import requests
11 | import re
12 | import matplotlib.pyplot as plt
13 | from Weather import Ui_Form
14 | from PyQt5.QtWidgets import QMainWindow, QApplication
15 | from PyQt5.QtGui import QPixmap
16 | from PyQt5.QtCore import Qt
17 | plt.rcParams['font.sans-serif'] = ['SimHei']
18 | plt.rcParams['axes.unicode_minus'] = False
19 |
20 | my_key = '*******'
21 |
22 |
23 | class Weather_Info(QMainWindow, Ui_Form):
24 | def __init__(self, parent=None):
25 | super(Weather_Info, self).__init__(parent)
26 | self.setupUi(self)
27 | self.img.setAlignment(Qt.AlignCenter)
28 | self.loc_scan.clicked.connect(lambda: self.get_lon_lat(
29 | location_name=self.position.text(),
30 | ))
31 | self.loc_scan.clicked.connect(lambda: self.get_weather(
32 | lng=self.longitude.text(),
33 | lat=self.latitude.text(),
34 | state=1,
35 | ))
36 | self.temp_radioButton.toggled.connect(lambda: self.get_weather(
37 | lng=self.longitude.text(),
38 | lat=self.latitude.text(),
39 | state=2,
40 | ))
41 | self.radioButton.toggled.connect(lambda: self.get_weather(
42 | lng=self.longitude.text(),
43 | lat=self.latitude.text(),
44 | state=3,
45 | ))
46 |
47 | # 根据地点名称获取其经纬度以及简单的描述信息
48 | def get_lon_lat(self, location_name):
49 | location_api = 'https://api.map.baidu.com/geocoding/v3/?address={}&output=json&ak={}&callback=showLocation'.format(
50 | location_name, my_key)
51 | try:
52 | res = requests.get(location_api)
53 | res = res.text
54 | except Exception as e:
55 | print(e)
56 | res = eval(re.findall('{.*}', res)[0])
57 | self.longitude.setText(str(round(res['result']['location']['lng'], 6)))
58 | self.latitude.setText(str(round(res['result']['location']['lat'], 6)))
59 | self.information.setText(str(res['result']['level']))
60 |
61 | # 根据经纬度获取天气信息
62 | def get_weather(self, lng, lat, state):
63 | weather_api = 'https://res.abeim.cn/api-weather?lng={}&lat={}'.format(lng, lat)
64 | try:
65 | res = requests.get(weather_api)
66 | res = res.json()
67 | except Exception as e:
68 | print(e)
69 | if state==1:
70 | self.temp_location.setText(res['地址'])
71 | self.time_stamp.setText(res['更新时间'])
72 | self.temp_now.setText(res['现在']['温度'])
73 | self.humidity_now.setText(res['现在']['湿度'])
74 | self.pressure.setText(res['现在']['气压'])
75 | elif state==2:
76 | temp = [i['温度'] for i in res['小时预报']['温度']]
77 | plt.figure()
78 | plt.plot(list(range(1, len(temp)+1)), temp)
79 | plt.xlabel('距离现在多少个小时')
80 | plt.ylabel('温度')
81 | plt.grid()
82 | plt.savefig('img/1.jpg')
83 | self.img.setPixmap(QPixmap('img/1.jpg'))
84 | elif state==3:
85 | temp = [float(i['风速']) for i in res['小时预报']['风']]
86 | plt.figure()
87 | plt.plot(list(range(1, len(temp)+1)), temp)
88 | plt.xlabel('距离现在多少个小时')
89 | plt.ylabel('风速')
90 | plt.grid()
91 | plt.savefig('img/2.jpg')
92 | self.img.setPixmap(QPixmap('img/2.jpg'))
93 |
94 |
95 | if __name__ == '__main__':
96 | app = QApplication(sys.argv)
97 | mainWindow = Weather_Info()
98 | mainWindow.show()
99 | sys.exit(app.exec_())
100 |
--------------------------------------------------------------------------------
/天气查询/天气.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzy6642/PyQt5/81f53a2e7fbae617290531139533142e481c776e/天气查询/天气.PNG
--------------------------------------------------------------------------------
/实时汇率换算/Exchange.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # Form implementation generated from reading ui file 'Exchange.ui'
4 | #
5 | # Created by: PyQt5 UI code generator 5.15.4
6 | #
7 | # WARNING: Any manual changes made to this file will be lost when pyuic5 is
8 | # run again. Do not edit this file unless you know what you are doing.
9 |
10 |
11 | from PyQt5 import QtCore, QtGui, QtWidgets
12 |
13 |
14 | class Ui_Form(object):
15 | def setupUi(self, Form):
16 | Form.setObjectName("Form")
17 | Form.resize(302, 153)
18 | self.layoutWidget = QtWidgets.QWidget(Form)
19 | self.layoutWidget.setGeometry(QtCore.QRect(11, 31, 267, 102))
20 | self.layoutWidget.setObjectName("layoutWidget")
21 | self.formLayout = QtWidgets.QFormLayout(self.layoutWidget)
22 | self.formLayout.setContentsMargins(0, 0, 0, 0)
23 | self.formLayout.setObjectName("formLayout")
24 | self.source = QtWidgets.QComboBox(self.layoutWidget)
25 | self.source.setObjectName("source")
26 | self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.source)
27 | self.source_value = QtWidgets.QDoubleSpinBox(self.layoutWidget)
28 | self.source_value.setObjectName("source_value")
29 | self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.source_value)
30 | self.target = QtWidgets.QComboBox(self.layoutWidget)
31 | self.target.setObjectName("target")
32 | self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.target)
33 | self.target_value = QtWidgets.QLineEdit(self.layoutWidget)
34 | self.target_value.setObjectName("target_value")
35 | self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.target_value)
36 | self.convert_button = QtWidgets.QPushButton(self.layoutWidget)
37 | self.convert_button.setObjectName("convert_button")
38 | self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.convert_button)
39 | self.line = QtWidgets.QFrame(self.layoutWidget)
40 | self.line.setFrameShape(QtWidgets.QFrame.HLine)
41 | self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
42 | self.line.setObjectName("line")
43 | self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.line)
44 |
45 | self.retranslateUi(Form)
46 | QtCore.QMetaObject.connectSlotsByName(Form)
47 |
48 | def retranslateUi(self, Form):
49 | _translate = QtCore.QCoreApplication.translate
50 | Form.setWindowTitle(_translate("Form", "Form"))
51 | self.convert_button.setText(_translate("Form", "转换"))
52 |
--------------------------------------------------------------------------------
/实时汇率换算/Exchange.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 302
10 | 153
11 |
12 |
13 |
14 | Form
15 |
16 |
17 |
18 |
19 | 11
20 | 31
21 | 267
22 | 102
23 |
24 |
25 |
26 | -
27 |
28 |
29 | -
30 |
31 |
32 | -
33 |
34 |
35 | -
36 |
37 |
38 | -
39 |
40 |
41 | 转换
42 |
43 |
44 |
45 | -
46 |
47 |
48 | Qt::Horizontal
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/实时汇率换算/run.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # @Time : 2021/8/10 8:41
3 | # @Author : Zhenyu Wu
4 | # @Email : wuzhenyuai@gmail.com
5 | # @File : run.py
6 | # @Software: PyCharm
7 | import sys
8 | import requests
9 | import re
10 | import pprint
11 | import pandas as pd
12 | from Exchange import Ui_Form
13 | from PyQt5.QtWidgets import QMainWindow, QApplication
14 |
15 |
16 | # 获取国际汇率
17 | def Get_Rate_All():
18 | rate = [i for i in requests.get('http://hl.anseo.cn/').text.replace('正无穷大', '99999').split('\n') if '人民币' in i and '1 ' in i][1:]
19 | rate_num = [re.findall('(\d+)(\.\d+)?', i) for i in rate]
20 | rate_num = [(float(''.join(i[0])), float(''.join(i[1]))) for i in rate_num]
21 | rate_dis = [re.findall('[\u4E00-\u9FA5]+', i)[:2] for i in rate]
22 | rate = [[rate_num[i][0], rate_dis[i][0], rate_num[i][1], rate_dis[i][1]] for i in range(len(rate_num))]
23 | rate = pd.DataFrame({'source_value': [i[0] for i in rate],
24 | 'source': [i[1] for i in rate],
25 | 'target_value': [i[2] for i in rate],
26 | 'target': [i[3] for i in rate],
27 | })
28 | return rate
29 |
30 |
31 | class Exchange_Rate(QMainWindow, Ui_Form):
32 | def __init__(self, parent=None):
33 | super(Exchange_Rate, self).__init__(parent)
34 | self.setupUi(self)
35 | self.rate_all = Get_Rate_All()
36 | self.add_item(self.rate_all)
37 | self.convert_button.clicked.connect(lambda: self.get_item(
38 | source=self.source.currentText(),
39 | target=self.target.currentText(),
40 | source_value=self.source_value.value(),
41 | rate_all=self.rate_all,
42 | ))
43 |
44 | # 为下拉框添加元素
45 | def add_item(self, rate_all):
46 | # all_items = list(set(list(self.rate_all['source'])+list(self.rate_all['target'])))
47 | all_items = list(set(list(self.rate_all['source'])))
48 | for i in range(len(all_items)):
49 | self.source.addItems(all_items)
50 | self.target.addItems(all_items)
51 | # self.source.setItemText(i, all_items[i])
52 | # self.target.setItemText(i, all_items[i])
53 |
54 | # 获取待转换的币种以及原始币种值
55 | def get_item(self, source, target, source_value, rate_all):
56 | if source != '人民币' and target != '人民币':
57 | rate_1 = list(rate_all[rate_all['source'].isin([source])]['target_value'])[0]
58 | rate_2 = list(rate_all[rate_all['target'].isin([target])]['target_value'])[0]
59 | convert = source_value * rate_1 * rate_2
60 | elif source == target:
61 | convert = source_value
62 | elif source == '人民币':
63 | rate = list(rate_all[rate_all['target'].isin([target])]['target_value'])[0]
64 | convert = source_value * rate
65 | elif target == '人民币':
66 | rate = list(rate_all[rate_all['source'].isin([source])]['target_value'])[0]
67 | convert = source_value * rate
68 | # pprint.pprint(convert)
69 | convert = round(convert, 3)
70 | self.target_value.setText(str(convert))
71 |
72 |
73 | if __name__ == '__main__':
74 | app = QApplication(sys.argv)
75 | mainWindow = Exchange_Rate()
76 | mainWindow.show()
77 | sys.exit(app.exec_())
78 |
--------------------------------------------------------------------------------
/实时汇率换算/汇率转换.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzy6642/PyQt5/81f53a2e7fbae617290531139533142e481c776e/实时汇率换算/汇率转换.PNG
--------------------------------------------------------------------------------
/项目模板/Template.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # Form implementation generated from reading ui file 'Template.ui'
4 | #
5 | # Created by: PyQt5 UI code generator 5.15.4
6 | #
7 | # WARNING: Any manual changes made to this file will be lost when pyuic5 is
8 | # run again. Do not edit this file unless you know what you are doing.
9 |
10 |
11 | from PyQt5 import QtCore, QtGui, QtWidgets
12 |
13 |
14 | class Ui_MainWindow(object):
15 | def setupUi(self, MainWindow):
16 | MainWindow.setObjectName("MainWindow")
17 | MainWindow.resize(306, 162)
18 | self.centralwidget = QtWidgets.QWidget(MainWindow)
19 | self.centralwidget.setObjectName("centralwidget")
20 | MainWindow.setCentralWidget(self.centralwidget)
21 | self.menubar = QtWidgets.QMenuBar(MainWindow)
22 | self.menubar.setGeometry(QtCore.QRect(0, 0, 306, 26))
23 | self.menubar.setObjectName("menubar")
24 | MainWindow.setMenuBar(self.menubar)
25 | self.statusbar = QtWidgets.QStatusBar(MainWindow)
26 | self.statusbar.setObjectName("statusbar")
27 | MainWindow.setStatusBar(self.statusbar)
28 |
29 | self.retranslateUi(MainWindow)
30 | QtCore.QMetaObject.connectSlotsByName(MainWindow)
31 |
32 | def retranslateUi(self, MainWindow):
33 | _translate = QtCore.QCoreApplication.translate
34 | MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
35 |
--------------------------------------------------------------------------------
/项目模板/Template.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 306
10 | 162
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/项目模板/__pycache__/Template.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzy6642/PyQt5/81f53a2e7fbae617290531139533142e481c776e/项目模板/__pycache__/Template.cpython-39.pyc
--------------------------------------------------------------------------------
/项目模板/main.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | # @Time : 2021/8/11 9:12
3 | # @Author : Zhenyu Wu
4 | # @Email : wuzhenyuai@gmail.com
5 | # @File : main.py
6 | # @Software: PyCharm
7 | # 顺序:
8 | # 1、Tools -> External Tools -> QtDesigner(绘制界面) -> 保存为`UI文件名.ui`
9 | # 2、右击`UI文件名.ui` -> External Tools -> PyUIC -> 生成`UI文件名.py`
10 | # 3、右击项目文件夹 -> Mark Directory as -> Sources Root
11 | # 4、新建`main.py` -> 添加下述代码并运行
12 | # 界面与逻辑分离
13 | import sys
14 | from PyQt5.QtWidgets import QApplication, QMainWindow
15 | from Template import Ui_MainWindow # 导入UI类
16 |
17 |
18 | # 新建类继承于UI类,方便进一步书写逻辑
19 | class Application(QMainWindow, Ui_MainWindow):
20 | def __init__(self, parent=None):
21 | super(Application, self).__init__(parent)
22 | self.setupUi(self)
23 |
24 |
25 | if __name__ == '__main__':
26 | app = QApplication(sys.argv) # 创建应用程序对象
27 | mainWindow = Application() # 实例化界面
28 | mainWindow.show() # 窗口显示
29 | sys.exit(app.exec_()) # 主循环结束
30 |
--------------------------------------------------------------------------------