当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 使用PtQt5实现桌面程序

使用PtQt5实现桌面程序

2020年07月12日  | 移动技术网IT编程  | 我要评论

使PtQt5使用PtQt5实现桌面程序

视频教程:B站、网易云课堂、腾讯课堂
代码地址:Gitee、Github
存储地址:
Google云

百度云:https://pan.baidu.com/s/14P4SFSUGPo6ln3GJHEHUwQ
提取码:q2n9


一 前期须知:


  • 安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5-tools
#pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5

二 简单样例

main.py

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
from PyQt5.QtCore import pyqtSlot


class App(QWidget):
    def __init__(self):
        super().__init__()
        #**********************定义一些初始值****************************
        # 下面的常用数值都可以放到这里来
        self.width=1000
        self.height=800
        self.initUI()

    def initUI(self):
        # **********************定义一些初始组件****************************
        # self 在这里就可以看作是窗体
        # 窗体属性设置
        self.setWindowTitle("智能医疗诊断系统") # 标题
        self.resize(self.width, self.height) # 窗体大小
        self.move(200, 200)  # 窗体位置(相对于整个屏幕)

        # label组件
        self.label = QLabel(self)
        self.label.setText("null")
        self.label.move(200, 200)
        self.label.resize(200,100)


        # button组件
        """在窗体内创建button对象"""
        self.button = QPushButton("诊断", self)
        """方法setToolTip在用户将鼠标停留在按钮上时显示的消息"""
        self.button.setToolTip("This is an example button")
        """按钮坐标x = 100, y = 70"""
        self.button.move(100, 70)
        """按钮与鼠标点击事件相关联"""
        self.button.clicked.connect(self.on_click)

        self.show()



    # **********************定义一些事件 ****************************
    """创建鼠标点击事件"""
    @pyqtSlot()
    def on_click(self):
        self.label.setText("hello!   major_s")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

结果:

在这里插入图片描述
双击exe:
在这里插入图片描述
点击诊断:

在这里插入图片描述

三 打包

pyinstaller -F -w  -i icon/icon.ico  -n 智能医疗诊断系统  main.py
-F 表示生成单个可执行文件
-w 表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!
-p 表示你自己自定义需要加载的类路径,一般情况下用不到
-i 表示可执行文件的图标
-n 起名

四 调用远程微服务

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
from PyQt5.QtCore import pyqtSlot
import requests
import os

class App(QWidget):
    def __init__(self):
        super().__init__()
        #**********************定义一些初始值****************************
        # 下面的常用数值都可以放到这里来
        self.width=1000
        self.height=800
        self.initUI()

    def initUI(self):
        # **********************定义一些初始组件****************************
        # self 在这里就可以看作是窗体
        # 窗体属性设置
        self.setWindowTitle("智能医疗诊断系统") # 标题
        self.resize(self.width, self.height) # 窗体大小
        self.move(200, 200)  # 窗体位置(相对于整个屏幕)

        # label组件
        self.label = QLabel(self)
        self.label.setText("null")
        self.label.move(200, 200)
        self.label.resize(500,100)


        # button组件
        """在窗体内创建button对象"""
        self.button = QPushButton("诊断", self)
        """方法setToolTip在用户将鼠标停留在按钮上时显示的消息"""
        self.button.setToolTip("This is an example button")
        """按钮坐标x = 100, y = 70"""
        self.button.move(100, 70)
        """按钮与鼠标点击事件相关联"""
        self.button.clicked.connect(self.on_click)

        self.show()



    # **********************定义一些事件 ****************************
    """创建鼠标点击事件"""
    @pyqtSlot()
    def on_click(self):
        # self.label.setText("hello!   major_s")
        url = "http://***.***.***.***:5000"
        imageFilePath='./test/0MEDOLBT.jpg'
        imageFileName = os.path.split(imageFilePath)[1]
        file_dict = {
            'file': (imageFileName, open(imageFilePath, 'rb'), 'image/jpg')
        }
        result = requests.post(url, files=file_dict)
        predict_result = result.text
        self.label.setText('图片路径:%s 预测结果为:%s\n' % (imageFilePath, predict_result))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

结果:
在这里插入图片描述


五 推荐视频教程

https://www.bilibili.com/video/BV154411n79k
https://www.bilibili.com/video/BV1UZ4y1p7PA

png等转ico

PyInstaller打包Python项目详解

本文地址:https://blog.csdn.net/qq_41375318/article/details/107246370

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网