Swim正在使用pyqt5创建一个gui。在那个gui我有一个基本程序来读取两个lineEdits,并使用它们循环并打印一些基本输出。我想要做的是能够通过单击按钮编辑/添加在其功能中间运行的变量。 在我的脑海中,我将不得不添加一些线程创建并使用IPC或其他东西。 主要问题是,当使用Qt5时,如果按钮单击导致函数运行,我如何与该功能交互运行与另一个按钮单击? 谢谢。
##Have to come up with way to have all
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout, QGridLayout
from PyQt5.QtWidgets import QLabel, QLineEdit, QComboBox
from PyQt5 import QtCore
from time import sleep
import serial
from serial.serialutil import SerialException
from serialUtil import full_port_name, enumerate_serial_ports
from helpers import RepresentsInt
mustBeInt = "MUST BE AN INTEGER"
class MainWindow(QDialog):
def __init__(self):
super().__init__()
self.title = 'Serial Test GUI'
self.left = 50
self.top = 50
self.width = 320
self.height = 100
self.comms = []
self.defaultIterations = 20
self.defaultTimeout = 500
self.initUI()
return
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
#self.createGridLayout()
#windowLayout = QVBoxLayout()
windowLayout = QGridLayout()
#windowLayout.addWidget(QPushButton('1'),0,0)
#windowLayout.addWidget(QPushButton('2'),0,1)
#windowLayout.addWidget(QPushButton('Stop'),2,2)
self.labelComs = QLabel("COMs:")
self.labelIterations = QLabel("Iterations: ")
self.labelTimeout = QLabel("Timeout(in ms): ")
windowLayout.addWidget(self.labelComs, 0,0, alignment=QtCore.Qt.AlignRight)
windowLayout.addWidget(self.labelIterations, 1,0, alignment=QtCore.Qt.AlignRight)
windowLayout.addWidget(self.labelTimeout, 2,0, alignment=QtCore.Qt.AlignRight)
self.comboboxComs = QComboBox()
ports = self.populateComsBox(self.comboboxComs)
windowLayout.addWidget(self.comboboxComs, 0,1)
self.lineIterations = QLineEdit()
self.lineTimeout = QLineEdit()
self.lineIterations.setText(str(self.defaultIterations))
self.lineTimeout.setText(str(self.defaultTimeout))
windowLayout.addWidget(self.lineIterations, 1,1,1,2)
windowLayout.addWidget(self.lineTimeout, 2,1,1,2)
#windowLayout.addWidget(self.lineTimeout, 2,1)
self.buttonStart = QPushButton('Start')
self.buttonStop = QPushButton('Stop')
self.buttonStart.clicked.connect(self.startClick)
windowLayout.addWidget(self.buttonStop, 3,2)
windowLayout.addWidget(self.buttonStart, 3,0)
self.setLayout(windowLayout)
#windowLayout.addWidget(self.lineIterations, 1,1,2,1,PyQt5.AlignCenter)
#windowLayout.addWidget(self.lineIterations, 1,1)
#self.lineIterations.setAlignment(QtCore.Qt.AlignCenter)//Makes the entry into the text lineEdit align to the center.
#windowLayout.addWidget(self.lineIterations, 1,1)
def startClick(self):
if RepresentsInt(self.lineIterations.text()) != True:
self.lineIterations.setText(mustBeInt)
if RepresentsInt(self.lineTimeout.text()) != True:
self.lineTimeout.setText(mustBeInt)
if self.lineIterations.text()==mustBeInt or self.lineTimeout.text()==mustBeInt:
return
for i in range(0, int(self.lineIterations.text())):
print("Iteration " + str(i+1) + " of " + self.lineIterations.text())
sleep(int(self.lineTimeout.text())/1000.0)
return
def populateComsBox(self, comboBox):
if 'win' in sys.platform:
for portname in enumerate_serial_ports():
self.comms.append(portname)
elif 'linux' in sys.platform:
pass
return
else:
pass
return
self.comms.sort()
for port in self.comms:
comboBox.addItem(port);
return
if __name__ == '__main__':
app = QApplication(sys.argv)
screen = MainWindow()
screen.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
启动按钮单击对变量所做的工作的功能应该与工作的功能分开。这类问题的基本块是这样的:
def StarterFunction(self):
WorkThread = threading.Thread(target=self.WorkerFunction, args=(tuple, of, arguments))
WorkThread.start()
while WorkThread.is_alive():
QApplication.processEvents() # This keeps the GUI responsive.
WorkThread.join()
def WorkerFunction(self):
# do some work with variables
对于您的具体问题,如果我理解正确的话,您希望在工作函数运行时启动所有按钮以更新变量吗?然后你可以这样做:
def StarterFunction(self):
self.ui.button.clicked.disconnect(self.StarterFunction)
self.ui.button.clicked.connect(self.UpdateMyVariables)
WorkThread = threading.Thread(target=self.WorkerFunction, args=(tuple, of, arguments))
WorkThread.start()
while WorkThread .is_alive():
QApplication.processEvents() # This keeps the GUI responsive.
WorkThread.join()
self.ui.button.clicked.connect(self.StarterFunction)
self.ui.button.clicked.disconnect(self.UpdateMyVariables)
def WorkerFunction(self):
# do some work with variables
def UpdateMyVariables(self):
# read inputs from the GUI
self.MyVariable1 = self.ui.lineIterations.text()
self.MyVariable2 = self.ui.lineTimeout.text()
最后一部分要求您将WorkFunction()
中处理的变量声明为__init__
中GUI类的属性,以便WorkFunction()
和{{}可以访问和修改它们1}}。