使用python+pyqt5开发桌面工具

 

环境:python3+pyqt5+qtdesigner

步骤:

1.先用qtdesigner画好图形界面

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Z2H6KGh5pWZ5rS-Lg==,size_20,color_FFFFFF,t_70,g_se,x_16

2.将画好的页面转换为.py文件

pyuic5 -o demo_weather.py demo_weather.ui

添加

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()  # ui_from是类名
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

3.给按钮添加自己写的方法

        self.retranslateUi(MainWindow)
        self.pushButton_2.clicked.connect(MainWindow.close)
        self.pushButton.clicked.connect(self.weather)
        self.pushButton_3.clicked.connect(self.textEdit.clear)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

4.后续需要exe文件的,使用pyinstaller将python文件打包成exe程序

pyinstaller -F xxx.py

完整的事例(对应的接口key已过期,可上阿里云申请)

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

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import urllib.request
import json

from PyQt5 import QtCore, QtGui, QtWidgets

class EmittingStream(QtCore.QObject):
    textWritten = QtCore.pyqtSignal(str)

    def write(self, text):
        self.textWritten.emit(str(text))
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(493, 403)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(60, 20, 381, 41))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.label.setObjectName("label")
        self.horizontalLayout_2.addWidget(self.label)
        self.lineEdit = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
        self.lineEdit.setObjectName("lineEdit")
        self.horizontalLayout_2.addWidget(self.lineEdit)
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(50, 70, 411, 231))
        self.textEdit.setObjectName("textEdit")

        sys.stdout = EmittingStream(textWritten=self.outputWritten)
        sys.stderr = EmittingStream(textWritten=self.outputWritten)


        self.horizontalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(20, 310, 461, 71))
        self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.pushButton_3 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
        self.pushButton_3.setObjectName("pushButton_3")
        self.horizontalLayout.addWidget(self.pushButton_3)
        self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout.addWidget(self.pushButton_2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 493, 23))
        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.pushButton_2.clicked.connect(MainWindow.close)
        self.pushButton.clicked.connect(self.weather)
        self.pushButton_3.clicked.connect(self.textEdit.clear)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "请输入需要查询的城市天气:"))
        self.pushButton.setText(_translate("MainWindow", "查询"))
        self.pushButton_3.setText(_translate("MainWindow", "清除"))
        self.pushButton_2.setText(_translate("MainWindow", "退出"))

    def outputWritten(self, text):
        cursor = self.textEdit.textCursor()
        cursor.movePosition(QtGui.QTextCursor.End)
        cursor.insertText(text)
        self.textEdit.setTextCursor(cursor)
        self.textEdit.ensureCursorVisible()


    def weather(self):
        area = self.lineEdit.text()
        if area == '':
            print("提示;请输入要查询的城市")
        else:

            area = urllib.request.quote(area)

            host = 'https://saweather.market.alicloudapi.com'
            path = '/spot-to-weather'
            method = 'GET'
            appcode = '73122fbc01f44c67a2a479659d90ac71'
            # https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&srcqid=2465158997087557668&tn=50000021_hao_pg&wd=%E4%B8%BD%E6%B1%9F&oq=%25E9%2598%25BF%25E9%2587%258C%25E4%25BA%2591&rsv_pq=ac089ce300038f23&rsv_t=1f53%2B8kwXtZJgIvr3Gvlci50MQ%2FYKcfzaz3zOksIySO7wYKtEkTo9e54pL0hiZuvvcdJ1UaB&rqlang=cn&rsv_enter=1&inputT=1574&rsv_sug3=18&rsv_sug1=12&rsv_sug7=100&rsv_sug2=0&rsv_sug4=1574
            querys = querys = 'area=' + area

            url = host + path + '?' + querys
            # 对象Request
            request = urllib.request.Request(url)

            # 模拟浏览器请求
            request.add_header('Authorization', 'APPCODE ' + appcode)

            response = urllib.request.urlopen(request, )
            content = response.read()

            # 解码:
            info = content.decode()
            info = json.loads(info)
            #print(type(info))
            # print(info['showapi_res_body'])
            # lis.delete(0,END)
            # lis.insert(0,"天气预报:%s"%info['showapi_res_body'])
            dict1 = info['showapi_res_body']
            # print(dict1)
            dict2 = dict1['f2']
            # print(dict2)
            print(("空气温度:%s" % dict2['night_air_temperature']))
            print(("白天风级:%s" % dict2['day_wind_power']))
            print(("夜晚风级:%s" % dict2['night_wind_power']))
            print(("天气情况:%s" % dict2['day_weather']))
            print(( "风向:%s" % dict2['day_wind_direction']))
            print(( "紫外线强度:%s" % dict2['ziwaixian']))
            print(( "日出日落:%s" % dict2['sun_begin_end']))
            print(("天气图片:%s" % dict2['day_weather_pic']))

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    #QMainWindow窗口也带有最大化最小化和退出,另外窗口最下面有statusbar可以显示设置的消息
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()  # Ui_MainWindow是类名
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

效果展示:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Z2H6KGh5pWZ5rS-Lg==,size_15,color_FFFFFF,t_70,g_se,x_16

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

均衡教派.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值