Pyqt5-PySide2

1,下拉框前端和后台关联

py文件

import sys

from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication


class my_form():
    def __init__(self):
        qfile_stats = QFile(r"E:\Work\space\raspberry\ui\power.ui")
        qfile_stats.open(QFile.ReadOnly)
        qfile_stats.close()

        self.ui = QUiLoader().load(qfile_stats)
        self.ui.comboBox.activated.connect(self.comboActivated)

    def comboActivated(self, index):
        if index == -1:
            print("Clicked on dropdown button")
        else:
            print("Selected option", self.ui.comboBox.itemText(index))


if __name__ == '__main__':
    app = QApplication([])
    main = my_form()
    main.ui.show()
    app.exec_()
View Code

ui文件:使用Qt designer制作的下拉框控件

 

2,日志实时滚动显示在界面上

import sys
from PySide2.QtWidgets import QApplication, QMainWindow, QTextEdit, QVBoxLayout, QWidget
from PySide2.QtCore import QThread, Signal, QTimer


class LogThread(QThread):
    log_signal = Signal(str)

    def __init__(self):
        super().__init__()
        self.logs = []

    def run(self):
        while True:
            log_message = f"Log: {self.logs.__len__()}"
            self.logs.append(log_message)
            self.log_signal.emit(log_message)
            self.msleep(1000)  # 每秒发送一次日志


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()
        self.thread = LogThread()
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_log)
        self.thread.log_signal.connect(self.on_log_received)

    def init_ui(self):
        self.setWindowTitle("Log Viewer")
        self.text_edit = QTextEdit()
        self.setCentralWidget(self.text_edit)
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

    def on_log_received(self, log_message):
        self.thread.logs.append(log_message)
        self.timer.start(1000)  # 开始计时器,1000ms后触发timeout

    def update_log(self):
        self.timer.stop()  # 停止计时器
        if self.thread.logs:
            log_message = self.thread.logs.pop()
            self.text_edit.append(log_message)

    def showEvent(self, event):
        self.thread.start()
        self.timer.start(1000)

    def closeEvent(self, event):
        self.thread.terminate()
        self.thread.wait()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
View Code

 

 3,结果实时显示在界面上

import os
import sys
import threading
import time

from PyQt5.QtCore import pyqtSignal, QThread
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QVBoxLayout, QWidget


from ip import Ui_Form


class CheckFunc(QMainWindow, Ui_Form):


    def __init__(self):
        super(CheckFunc, self).__init__()

        self.setupUi(self)

        self.pushButton.clicked.connect(self.start_ping)

        self.myth = mythread()

        self.myth.my_ip_info.connect(self.set_ipinfo_to_ui)

    def set_ipinfo_to_ui(self, str):
        self.textBrowser.setText(str)

    def start_ping(self):
        self.myth.start()


class mythread(QThread):
    my_ip_info = pyqtSignal(str)

    def __init__(self):
        self.ip_info = ''
        super(mythread, self).__init__()

    def work(self, ip):
        os.popen(f'ping {ip}')
        res = os.popen(f'ping {ip}')

        if 'TTL' in res.read():
            self.ip_info = self.ip_info + ip + '在线\n'
        else:
            self.ip_info = self.ip_info + ip + '不在线\n'

    def gevent_ping(self):
        t1 = time.time()

        thread_pool = []

        for i in range(1, 10):
            t = threading.Thread(target=self.work, args=(f'172.80.100.{i}',))
            thread_pool.append(t)

        for t in thread_pool:
            t.start()
        for t in thread_pool:
            t.join()

        t2 = time.time()

        print(f'运行时间 = {t2 - t1}')

    def run(self):
        self.gevent_ping()
        self.my_ip_info.emit(self.ip_info)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = CheckFunc()
    window.show()
    sys.exit(app.exec_())
View Code
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ip.ui'
#
# Created by: PyQt5 UI code generator 5.15.10
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(879, 653)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(70, 100, 51, 41))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(140, 110, 31, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(Form)
        self.lineEdit_2.setGeometry(QtCore.QRect(190, 110, 31, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(Form)
        self.lineEdit_3.setGeometry(QtCore.QRect(240, 110, 31, 20))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.lineEdit_4 = QtWidgets.QLineEdit(Form)
        self.lineEdit_4.setGeometry(QtCore.QRect(290, 110, 31, 20))
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.lineEdit_5 = QtWidgets.QLineEdit(Form)
        self.lineEdit_5.setGeometry(QtCore.QRect(440, 110, 31, 20))
        self.lineEdit_5.setObjectName("lineEdit_5")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(370, 100, 61, 41))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(600, 100, 101, 41))
        self.pushButton.setObjectName("pushButton")
        self.textBrowser = QtWidgets.QTextBrowser(Form)
        self.textBrowser.setGeometry(QtCore.QRect(70, 230, 351, 181))
        self.textBrowser.setObjectName("textBrowser")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(80, 180, 51, 41))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "输入ip:"))
        self.label_2.setText(_translate("Form", "掩码:"))
        self.pushButton.setText(_translate("Form", "开始检测"))
        self.textBrowser.setHtml(_translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
        self.label_3.setText(_translate("Form", "结果:"))
ip.py

 

posted @ 2024-02-23 14:48  夜未央leo  阅读(13)  评论(0编辑  收藏  举报