pyqt实战之带你写个网络测试程序

一、准备阶段

请看这篇文章:http://blog.csdn.net/ruoyunliufeng/article/details/77921361

1.补充pysinstall和pycharm的集成:

1.1pip安装pyinstall

pip install pyinstall

1.2按照下图配置


3.使用


二、程序编写

1.设计UI

在当前问津新建.ui文件(前提是你已经完成上面的安装步骤),然后右击文件选择外部工具的QTdesigner


QTdesigner的设计部分,我就不多讲了,可能后续会出博客专题专门讲。大体原理就是拖拖拽拽改改参数。


2.将UI程序转换成python程序


转换后注释三行代码:

        # MainWindow.setCentralWidget(self.centralwidget)
        # MainWindow.setStatusBar(self.statusbar)
        # MainWindow.setMenuBar(self.menuBar)



主程序

# encoding: utf-8

# ***网络测试程序V0.1***
# 1.采用ping原理进行网络测试
# 2.分离UI主线程和工作线程
# ***********************
# V0.2
# 更新网络成功或失败按钮颜色的变化


import os
import sys
import time


global ip



from ui_net import Ui_MainWindow
from PyQt5 import QtWidgets,QtCore,Qt
from PyQt5.QtGui import QPixmap,QPalette
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class WorkThread(QtCore.QThread):
    sinOut = pyqtSignal(int)
    def __int__(self):
        super(WorkThread,self).__init__()


    def run(self ):
        global ip
        exit_code = os.system(u'ping %s'%ip)
        if exit_code:
            # raise Exception('connect failed.')
            self.sinOut.emit(0)    #反馈信号出去
        else:
            self.sinOut.emit(1) #反馈信号出去


# 
class mywindow(QtWidgets.QWidget, Ui_MainWindow):
    trigger = QtCore.pyqtSignal()
    def __init__(self):
        super(mywindow, self).__init__()
        self.setupUi(self)

        # 连接 QPushButton 的点击信号到槽 BigWork()
        self.pushButton.clicked.connect(self.net_work)

        # # 延时为了看启动界面
        # time.sleep(1)

    def net_work(self):
        # print("开始多线程")

        self.wt = WorkThread()
        global  ip
        ip =  self.lineEdit.text()
        print (ip)
        #开始执行run()函数里的内容
        self.wt.start()

        # # 收到信号
        self.wt.sinOut.connect(self.net_back)  #将信号连接至net_back函数

    def net_back(self,net_bool):
        if net_bool ==1:
            self.pushButton.setText(u"成功")
            # button1.setStyleSheet("background-color: red");
            # button2.setStyleSheet("background-color:#ff0000;");
            # button3.setStyleSheet("background-color:rgb(255,0,0)");
            self.pushButton.setStyleSheet("background-color: green")

        else:
            self.pushButton.setText(u"失败")
            self.pushButton.setStyleSheet("background-color: red")

if __name__=="__main__":
    app=QtWidgets.QApplication(sys.argv)

    # 启动界面
    splash = QtWidgets.QSplashScreen(QPixmap("D://D.png"))
    splash.show()
    splash.showMessage(u"正在加载软件",alignment = QtCore.Qt.AlignHCenter)

    app.processEvents() # 防止卡死
    print("app.processEvents()")
    myshow=mywindow()
    print("app.processEvents()")
    myshow.show()
    print("app.processEvents()")

    # 关闭启动界面
    splash.finish(myshow)

    sys.exit(app.exec_())



程序有几个要点:

1.开机界面实现。

2.线程实现,方式运行程序过程中UI界面的卡死。

3.调用windows命令行的实现。


三、效果




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

若云流风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值