用QtDesigner工具进行界面绘制,注意各种插件的命名,以免生成的代码自己都看不懂。
绘制好之后,再利用PyUIC把.ui文件转为.py文件。
PyCharm可以集成QtDesigner PyUIC这两种工具。
func.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'func.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!
from subprocess import Popen
from threading import Thread
import time
from PyQt5 import QtCore, QtGui, QtWidgets
import subprocess
import threading
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1014, 695)
# self.ads = QtWidgets.QWidget(MainWindow)
# self.ads.setObjectName("ads")
self.horizontalLayoutWidget = QtWidgets.QWidget()
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(50, 80, 871, 521))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout_5 = QtWidgets.QVBoxLayout()
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.driverWin_label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.driverWin_label.setObjectName("driverWin_label")
self.verticalLayout_5.addWidget(self.driverWin_label, 0, QtCore.Qt.AlignHCenter)
self.driver_win = QtWidgets.QTextBrowser(self.horizontalLayoutWidget)
self.driver_win.setObjectName("textBrowser")
self.verticalLayout_5.addWidget(self.driver_win)
self.horizontalLayout.addLayout(self.verticalLayout_5)
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.label.setText("")
self.label.setObjectName("label")
self.verticalLayout_3.addWidget(self.label)
self.start_button = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.start_button.setObjectName("start_button")
self.verticalLayout_3.addWidget(self.start_button)
self.stop_button = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.stop_button.setObjectName("stop_button")
self.verticalLayout_3.addWidget(self.stop_button)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout_3.addItem(spacerItem)
self.horizontalLayout.addLayout(self.verticalLayout_3)
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.armWin_label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.armWin_label.setObjectName("armWin_label")
self.verticalLayout_4.addWidget(self.armWin_label, 0, QtCore.Qt.AlignHCenter)
self.armWin = QtWidgets.QTextBrowser(self.horizontalLayoutWidget)
self.armWin.setObjectName("armWin")
self.verticalLayout_4.addWidget(self.armWin)
self.transmitWin_label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.transmitWin_label.setObjectName("transmitWin_label")
self.verticalLayout_4.addWidget(self.transmitWin_label, 0, QtCore.Qt.AlignHCenter)
self.transmitWin = QtWidgets.QTextBrowser(self.horizontalLayoutWidget)
self.transmitWin.setObjectName("transmitWin")
self.verticalLayout_4.addWidget(self.transmitWin)
self.horizontalLayout.addLayout(self.verticalLayout_4)
MainWindow.setCentralWidget(self.horizontalLayoutWidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1014, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#self.driver_win.append("%s" % '111')
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.driverWin_label.setText(_translate("MainWindow", "driver window"))
self.start_button.setText(_translate("MainWindow", "start"))
self.stop_button.setText(_translate("MainWindow", "stop"))
self.armWin_label.setText(_translate("MainWindow", "arm window"))
self.transmitWin_label.setText(_translate("MainWindow", "transmit window"))
class show_thred(QThread):
sinOut = pyqtSignal(str)
def __init__(self,show_win,data):
self.data = data
self.show_win = show_win
super().__init__()
def run(self):
while self.data.poll is not None:
#self.show_win.append(str(self.data.stdout.readline()))
outputdata = self.data.stdout.readline()
self.sinOut.emit(str(outputdata))
class func_ui(Ui_MainWindow):
driver_thread = ... # type: Thread
data = ... # type: Popen
driver_show = ...
def __init__(self, MainWindow):
self.setupUi(MainWindow)
self.data = subprocess.Popen('./a.out', stdout=subprocess.PIPE, stdin=subprocess.PIPE,shell=True)
self.start_button.clicked.connect(self.start_button_click)
@pyqtSlot()
def start_button_click(self):
self.driver_show = show_thred(self.driver_win,self.data)
self.driver_show.sinOut.connect(self.show_driver_text)
self.driver_show.start()
def show_driver_text(self,msg):
print(msg)
self.driver_win.append(msg)
Ui_MainWindow和retranslateUi都是用PyUIC生成的,需要注意的是,自动生成的添加了一个窗口部件,我把它注释替换了。
原本的设计是想像tk库一样,在单独的线程中填充文本框,但是QT中,我没有实现这样的功能,在主线程创建的text部件,在另外的线程去填充内容会报警告,出现段错误。QT的C++版本提供了函数来解决这个问题,但是Python中我没有找到。所以采用了另外的方式。线程读取内容,然后再传递给主线程(UI线程),再在UI线程中添加内容到text中。
main.py:
#!/usr/bin/python3.5
import func
import sys
#引入模块,基本控件位于pyqt5.qtwidgets
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5 import QtCore, QtGui, QtWidgets
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = func.func_ui(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
效果图如上,布置界面的时候加入了各种布局(横纵),所以所有的部件都可以跟随窗口的大小改变大小
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/175136.html原文链接:https://javaforall.cn