how to use python script in pyqt5 ui widget code - python-3.x

I am having a pyhton script having some code as follows and and .ui file converted into the .py file. Now i want to execute the python script in widget code of gui widget?
Please help me how can i do that .
from PyQt5 import QtCore, QtGui, QtWidgets
import DC_gimbal as backend
import hudsup as hp
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(978, 600)
self.hudsup = hudsup()
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 291, 261))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.Heads_up = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.Heads_up.setContentsMargins(0, 0, 0, 0)
self.Heads_up.setObjectName("Heads_up")
self.widget = QtWidgets.QWidget(self.verticalLayoutWidget)
self.widget.setObjectName("widget")
self.HeadsWidget = QtWidgets.QWidget(self.widget)
self.HeadsWidget.setGeometry(QtCore.QRect(0, 0, 291, 261))
self.HeadsWidget.setObjectName("HeadsWidget")
self.Heads_up.addWidget(self.widget)
self.Heads_up.addWidget(self.huds_Up())
self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(0, 260, 291, 291))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.widget_3 = QtWidgets.QWidget(self.verticalLayoutWidget_2)
self.widget_3.setObjectName("widget_3")
self.displayWidget = QtWidgets.QWidget(self.widget_3)
self.displayWidget.setGeometry(QtCore.QRect(0, 0, 291, 281))
self.displayWidget.setObjectName("displayWidget")
self.verticalLayout.addWidget(self.widget_3)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(500, 10, 93, 28))
self.pushButton.setObjectName("connect")
self.pushButton.clicked.connect(backend.Noctua_initialise())
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(620, 10, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(740, 10, 93, 28))
self.pushButton_3.setObjectName("pushButton_3")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(290, 330, 561, 211))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 978, 26))
self.menubar.setObjectName("menubar")
self.menuconnect = QtWidgets.QMenu(self.menubar)
self.menuconnect.setObjectName("menuconnect")
self.menuDisconnect = QtWidgets.QMenu(self.menubar)
self.menuDisconnect.setObjectName("menuDisconnect")
self.menuMode = QtWidgets.QMenu(self.menubar)
self.menuMode.setObjectName("menuMode")
self.menuSensors = QtWidgets.QMenu(self.menubar)
self.menuSensors.setObjectName("menuSensors")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionLIDAR = QtWidgets.QAction(MainWindow)
self.actionLIDAR.setObjectName("actionLIDAR")
self.actionOpen_CV = QtWidgets.QAction(MainWindow)
self.actionOpen_CV.setObjectName("actionOpen_CV")
self.actionManual = QtWidgets.QAction(MainWindow)
self.actionManual.setObjectName("actionManual")
self.actionAuto = QtWidgets.QAction(MainWindow)
self.actionAuto.setObjectName("actionAuto")
self.actionSettings = QtWidgets.QAction(MainWindow)
self.actionSettings.setObjectName("actionSettings")
self.actionCalib = QtWidgets.QAction(MainWindow)
self.actionCalib.setObjectName("actionCalib")
self.actionDisconnect = QtWidgets.QAction(MainWindow)
self.actionDisconnect.setObjectName("actionDisconnect")
self.menuconnect.addAction(self.actionDisconnect)
self.menuDisconnect.addAction(self.actionSettings)
self.menuDisconnect.addAction(self.actionCalib)
self.menuMode.addAction(self.actionManual)
self.menuMode.addAction(self.actionAuto)
self.menuSensors.addAction(self.actionLIDAR)
self.menuSensors.addAction(self.actionOpen_CV)
self.menubar.addAction(self.menuconnect.menuAction())
self.menubar.addAction(self.menuDisconnect.menuAction())
self.menubar.addAction(self.menuMode.menuAction())
self.menubar.addAction(self.menuSensors.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
self.menuconnect.setTitle(_translate("MainWindow", "connect"))
self.menuDisconnect.setTitle(_translate("MainWindow", "configure"))
self.menuMode.setTitle(_translate("MainWindow", "Mode"))
self.menuSensors.setTitle(_translate("MainWindow", "Sensors"))
self.actionLIDAR.setText(_translate("MainWindow", "LIDAR"))
self.actionOpen_CV.setText(_translate("MainWindow", "Open CV"))
self.actionManual.setText(_translate("MainWindow", "Manual"))
self.actionAuto.setText(_translate("MainWindow", "Auto"))
self.actionSettings.setText(_translate("MainWindow", "Settings"))
self.actionCalib.setText(_translate("MainWindow", "Calib"))
self.actionDisconnect.setText(_translate("MainWindow", "Disconnect"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is my gui code and i want to add the below file in the headswidget and the file name is hudsup.py :
import sys
import math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
def rec_line_cord(i,list,xc,origin_list,pitch,roll):
.
.
def rec_drawLine(self,i,list,painter,roll,metrics):
.
.
.
def drawMarkings(self, painter):
.
.
.
def drawNeedle(self, painter,roll):
.
.
.
def Compass(self,painter,yaw,r):
.
.
.
class Manager(QObject):
changedValue = pyqtSignal(tuple)
def __init__(self):
QObject.__init__(self)
filename = "Attitude_data_Manual_1232018_1158.txt"
res = self.read_content(filename)
self.results = zip(*res)
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_value)
self.timer.start(100)
#pyqtSlot()
def update_value(self):
try:
self.changedValue.emit(next(self.results))
except StopIteration:
self.timer.stop()
def read_content(self, filename):
.
.
.
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.v = None
#pyqtSlot(tuple)
def update_value(self, v):
self.v = v
self.update()
def paintEvent(self, event):
QWidget.paintEvent(self, event)
painter = QPainter(self)
r = self.rect()
.
.
if __name__ == '__main__':
app = QApplication(sys.argv)
manager = Manager()
w = Widget()
manager.changedValue.connect(w.update_value)
w.show()
sys.exit(app.exec_())
This is my example of python script.
here is hudsup.py output :
animated huds_up display
and my ui looks like this and i want my hudsup to displayed as mentioned :
Gui

You should not use the generated code directly, instead, create a new widget class and inherit from it:
class MainWindow(Ui_MainWindow, QMainWindow):
def __init__(self):
Ui_MainWindow.__init__(self)
QMainWindow.__init__(self)
self.setupUi(self)
To integrate your second widget you can just add it to Heads_up like you are doing now but don't need to call exec_:
self.hud_sup = HudSup()
self.Heads_up.addWidget(self.hud_sup)

Related

I tried to use the mutex. Why did it automatically stop without an error

I tried to use the mutex. Why did it automatically stop without an error.
Running will stop automatically after a while
This is my code, which can be run directly. Please help me to see what the problem is
Is my method wrong? Is there a better way to achieve it? I don't want my interface to get stuck
`
import time
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QWaitCondition, QMutex
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QApplication, QDesktopWidget
from PyQt5.QtCore import pyqtSignal
import sys
from PyQt5.QtCore import QThread
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.pushButton_2.setText(_translate("MainWindow", "stop"))
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.__init2__()
def __init2__(self):
self.search_running = False
self.ui.pushButton.clicked.connect(self.search)
self.ui.pushButton_2.clicked.connect(self.stop_search)
def stop_search(self):
if self.search_running:
reply = QMessageBox.information(self, 'notice', 'Do you want to stop searching?',
QMessageBox.Yes | QMessageBox.Cancel)
if reply == QMessageBox.Yes:
self.search_thread.terminate()
self.__thread.resume()
self.search_running = False
self.ui.pushButton.setEnabled(True)
else:
QMessageBox.information(self, 'tip', 'no search!', QMessageBox.Ok)
def search(self):
if not self.search_running:
self.search_running = True
self.ui.pushButton.setEnabled(False)
self.search_thread = Search_Thread()
self.search_thread.trigger.connect(lambda i: self.c(i))
self.search_thread.complete.connect(self.search_complete)
self.search_thread.start()
def c(self, i):
self.ui.label.setText(str(i))
self.search_thread.resume()
def search_complete(self):
self.__thread.resume()
self.search_running = False
self.ui.pushButton.setEnabled(True)
class Search_Thread(QThread):
trigger = pyqtSignal(int)
complete = pyqtSignal()
mutex = QMutex()
cond = QWaitCondition()
def __init__(self):
super().__init__()
def run(self):
self.mutex.lock()
for i in range(10000):
for j in range(10000):
print(j)
for k in range(10000):
# time.sleep(0.1)
self.trigger.emit(i + j + k)
# time.sleep(0.1)
self.cond.wait(self.mutex)
self.complete.emit()
self.mutex.unlock()
def resume(self):
self.cond.wakeAll()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
screen = QDesktopWidget().screenGeometry()
height = 100
width = 300
window.setGeometry(int((screen.width() - width) / 2), int((screen.height() - height) / 2), width, height)
window.setWindowTitle("test")
window.show()
sys.exit(app.exec())
`
help me,How to improve the code so that it does not stop automatically

Buttons in PyQT5 using python

I created a back button in new window and next button in main window. After I clicked the next button to show a new window, it was successfully hides the current main window but when I clicked back button on the new window, the application automatically closes itself. Any idea?
The code in first mainWindow.py file:
from PyQt5 import QtCore, QtGui, QtWidgets
try:
from secondWindow import Ui_SecondWindow
except:
import secondWindow
class Ui_firstWindow(object):
def secondWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_SecondWindow()
self.ui.setupUi(self.window)
self.window.show()
if self.window.show():
self.hide
def setupUi(self, firstWindow):
firstWindow.setObjectName("firstWindow")
firstWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(firstWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_next = QtWidgets.QPushButton(self.centralwidget)
self.btn_next.setGeometry(QtCore.QRect(160, 100, 521, 291))
self.btn_next.setObjectName("btn_next")
######To Open second window###########
self.btn_next.clicked.connect(self.secondWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(360, 420, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
firstWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(firstWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
firstWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(firstWindow)
self.statusbar.setObjectName("statusbar")
firstWindow.setStatusBar(self.statusbar)
self.retranslateUi(firstWindow)
QtCore.QMetaObject.connectSlotsByName(firstWindow)
def retranslateUi(self, firstWindow):
_translate = QtCore.QCoreApplication.translate
firstWindow.setWindowTitle(_translate("firstWindow", "MainWindow"))
self.btn_next.setText(_translate("firstWindow", "Next"))
self.label.setText(_translate("firstWindow", "First Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
firstWindow = QtWidgets.QMainWindow()
ui = Ui_firstWindow()
ui.setupUi(firstWindow)
firstWindow.show()
sys.exit(app.exec_())
The codes in second secondWindow.py file:
from PyQt5 import QtCore, QtGui, QtWidgets
try:
from mainWindow import Ui_firstWindow
except:
import mainWindow
class Ui_SecondWindow(object):
###function to go back the first window######
# #pyqtSlot()
def openBackFirstWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_firstWindow()
self.ui.setupUi(self.window)
self.window.show()
if self.window.show():
self.hide
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_back = QtWidgets.QPushButton(self.centralwidget)
self.btn_back.setGeometry(QtCore.QRect(220, 100, 401, 281))
self.btn_back.setObjectName("btn_back")
###To go back to first windo######
self.btn_back.clicked.connect(self.openBackFirstWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(370, 440, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
SecondWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(SecondWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
SecondWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.btn_back.setText(_translate("SecondWindow", "Back"))
self.label.setText(_translate("SecondWindow", "Second Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
SecondWindow = QtWidgets.QMainWindow()
ui = Ui_SecondWindow()
ui.setupUi(SecondWindow)
SecondWindow.show()
sys.exit(app.exec_())
When i clicked the back button in second window, the application stopped working and exits itself. Any idea?
Do not modify the code generated by Qt Designer but create another class that inherits
from the appropriate widget and use the initial class to fill it.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_firstWindow(object):
def setupUi(self, firstWindow):
firstWindow.setObjectName("firstWindow")
firstWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(firstWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_next = QtWidgets.QPushButton(self.centralwidget)
self.btn_next.setGeometry(QtCore.QRect(160, 100, 521, 291))
self.btn_next.setObjectName("btn_next")
# ######To Open second window###########
# self.btn_next.clicked.connect(self.secondWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(360, 420, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
firstWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(firstWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
firstWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(firstWindow)
self.statusbar.setObjectName("statusbar")
firstWindow.setStatusBar(self.statusbar)
self.retranslateUi(firstWindow)
QtCore.QMetaObject.connectSlotsByName(firstWindow)
def retranslateUi(self, firstWindow):
_translate = QtCore.QCoreApplication.translate
firstWindow.setWindowTitle(_translate("firstWindow", "MainWindow"))
self.btn_next.setText(_translate("firstWindow", "Next"))
self.label.setText(_translate("firstWindow", "First Window"))
class Ui_SecondWindow(object):
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_back = QtWidgets.QPushButton(self.centralwidget)
self.btn_back.setGeometry(QtCore.QRect(220, 100, 401, 281))
self.btn_back.setObjectName("btn_back")
# ###To go back to first windo######
# self.btn_back.clicked.connect(self.openBackFirstWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(370, 440, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
SecondWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(SecondWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
SecondWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.btn_back.setText(_translate("SecondWindow", "Back"))
self.label.setText(_translate("SecondWindow", "Second Window"))
class SecondWindow(QtWidgets.QMainWindow, Ui_SecondWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
class MainWindow(QtWidgets.QMainWindow, Ui_firstWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
######To Open second window###########
self.btn_next.clicked.connect(self.secondWindow)
def secondWindow(self):
self.window = SecondWindow()
###To go back to first windo######
self.window.btn_back.clicked.connect(self.openBackFirstWindow)
self.window.show()
self.hide()
###function to go back the first window######
def openBackFirstWindow(self):
self.show()
self.window.hide()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
firstWindow = MainWindow()
firstWindow.show()
sys.exit(app.exec_())

Pyqt5 QFileDialog not working in my program for Getting Directories

I have This Qt MainWindow Shown Below:
I want my Browse button to Open a Dialog box for selecting Specific Directory.
I Went through the various post on Stack Overflow, I tried implementing the solution in the post, but It's not working for me.
Here is my Code :
from PyQt5 import QtCore, QtGui, QtWidgets
import os
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.start_button = QtWidgets.QPushButton(self.centralwidget)
self.start_button.setAutoFillBackground(False)
self.start_button.setAutoDefault(False)
self.start_button.setDefault(False)
self.start_button.setFlat(False)
self.start_button.setObjectName("start_button")
self.start_button.clicked.connect(lambda: self.start_button_click())
self.gridLayout_2.addWidget(self.start_button, 0, 0, 1, 1)
self.Br_button = QtWidgets.QPushButton(self.centralwidget)
self.Br_button.setObjectName("Br_button")
self.Br_button.clicked.connect(lambda: self.browse_button())
self.gridLayout_2.addWidget(self.Br_button, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.gridLayout_2.addWidget(self.label, 2, 1, 1, 1, QtCore.Qt.AlignHCenter)
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.gridLayout_2.addWidget(self.tableWidget, 3, 0, 1, 2)
self.gridLayout.addLayout(self.gridLayout_2, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.start_button.setToolTip(_translate("MainWindow", "Start The Program"))
self.start_button.setText(_translate("MainWindow", "Start"))
self.Br_button.setToolTip(_translate("MainWindow", "Browse the File Location to Watch on"))
self.Br_button.setText(_translate("MainWindow", "Browse"))
def start_button_click(self):
self.label.setText("Hello")
def browse_button(self):
fileName = QtWidgets.QFileDialog.getExistingDirectory(QtWidgets.QFileDialog,None,"Open Directory",os.getcwd(), QtWidgets.QFileDialog.ShowDirsOnly)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
When I Click on the Browse button it closes the Application abruptly after few seconds and I am Getting This Error Shown below:
Try it:
import sys
import os
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Form(QMainWindow):
def __init__(self,parent=None):
super().__init__(parent)
self.plainTextEdit = QPlainTextEdit()
self.plainTextEdit.setFont(QFont('Arial', 11))
openDirButton = QPushButton("Open Directory")
openDirButton.clicked.connect(self.browse_button)
layoutV = QVBoxLayout()
layoutV.addWidget(openDirButton)
layoutH = QHBoxLayout()
layoutH.addLayout(layoutV)
layoutH.addWidget(self.plainTextEdit)
centerWidget = QWidget()
centerWidget.setLayout(layoutH)
self.setCentralWidget(centerWidget)
def browse_button(self):
fileName = QFileDialog.getExistingDirectory(
#QtWidgets.QFileDialog, # ???
None,
"Open Directory",
os.getcwd(),
QFileDialog.ShowDirsOnly)
self.plainTextEdit.appendHtml("<br>Chose a folder: <b>{}</b>".format(fileName))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Form()
ex.resize(740,480)
ex.setWindowTitle("PyQt5-QFileDialog")
ex.show()
sys.exit(app.exec_())

Plot not updating qt GUI and pyqtgraph Python

As the title says, my plot does not update. The objetive is to initiate the plot when the button is clicked. The window pops up and the axis are formed, but no curve is plotted. i used pyqt5 in qt designer fr the GUI and pyqtgraph for the plot. if i wanted to add more plots in the same window whats the optimal way to do it. Thanks
Code:
from PyQt5 import QtCore, QtGui, QtWidgets
import os,serial,jeje_rc
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
import numpy as np
import pyqtgraph as pg
class Ui_MainWindow(object):
def ser(self):
self.raw = serial.Serial('COM4', 9600)
self.raw.close()
self.raw.open()
def update(self):
self.datos = self.raw.readline()
self.datos1 = self.datos.decode().split(',')
self.y1[self.m] = self.datos1[0]
if self.m == 99:
self.y1 = np.zeros(100, dtype=float)
self.m = 0
else:
self.m += 1
self.curva1.setData(self.y1)
app.processEvents()
def start(self):
self.ser()
self.win = pg.GraphicsWindow()
self.win.setWindowTitle('Datos de arduino')
self.p1 = self.win.addPlot()
self.p1.setYRange(0, 1024, padding=0)
self.curva1 = self.p1.plot()
self.datos = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
self.y1 = np.zeros(100, dtype=float)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowTitle("ECG de 12 Derivaciones UAO")
MainWindow.resize(800, 550)
self.m=0
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMaximumSize(QtCore.QSize(805, 510))
self.centralwidget.setObjectName("centralwidget")
self.centralwidget.setWindowTitle("ECG de 12 Derivaciones UAO")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 270, 241, 19))
self.label.setObjectName("label")
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(10, 230, 781, 20))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(600, 40, 181, 171))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(40, 10, 591, 41))
font = QtGui.QFont()
font.setPointSize(16)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(170, 150, 221, 41))
font = QtGui.QFont()
font.setPointSize(11)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(110, 190, 471, 41))
font = QtGui.QFont()
font.setPointSize(11)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(50, 100, 521, 51))
font = QtGui.QFont()
font.setPointSize(9)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.lineEdad = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdad.setGeometry(QtCore.QRect(110, 310, 261, 21))
self.lineEdad.setObjectName("lineEdad")
self.lineNombre = QtWidgets.QLineEdit(self.centralwidget)
self.lineNombre.setGeometry(QtCore.QRect(110, 270, 261, 20))
self.lineNombre.setObjectName("lineNombre")
self.BotonInicio = QtWidgets.QPushButton(self.centralwidget)
self.BotonInicio.setGeometry(QtCore.QRect(490, 290, 181, 41))
self.BotonInicio.setObjectName("BotonInicio")
self.BotonInicio.clicked.connect(self.start)
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(20, 310, 68, 19))
self.label_7.setObjectName("label_7")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(20, 350, 61, 19))
self.label_8.setObjectName("label_8")
self.label_9 = QtWidgets.QLabel(self.centralwidget)
self.label_9.setGeometry(QtCore.QRect(20, 380, 111, 31))
self.label_9.setObjectName("label_9")
self.BoxGenero = QtWidgets.QComboBox(self.centralwidget)
self.BoxGenero.setGeometry(QtCore.QRect(110, 350, 92, 25))
self.BoxGenero.setObjectName("BoxGenero")
self.BoxGenero.addItem("")
self.BoxGenero.addItem("")
self.plainTextPatologias = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextPatologias.setGeometry(QtCore.QRect(110, 390, 261, 91))
self.plainTextPatologias.setObjectName("plainTextPatologias")
self.line_2 = QtWidgets.QFrame(self.centralwidget)
self.line_2.setGeometry(QtCore.QRect(390, 250, 20, 241))
self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.BotonExportar = QtWidgets.QPushButton(self.centralwidget)
self.BotonExportar.setGeometry(QtCore.QRect(490, 410, 181, 41))
self.BotonExportar.setObjectName("BotonExportar")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 805, 31))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
timer = QtCore.QTimer()
timer.timeout.connect(self.update)
timer.start(0)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "ECG de 12 Derivaciones"))
self.label.setText(_translate("MainWindow", "Nombre:"))
self.label_2.setText(_translate("MainWindow", "<html><head/><body><p><img src=\":/newPrefix/logo-universidad-autonoma-de-occidente.png\"/></p></body></html>"))
self.label_3.setText(_translate("MainWindow", "Electrocardiógrafo De 12 Derivaciones"))
self.label_4.setText(_translate("MainWindow", "Facultad de ingenieria"))
self.label_5.setText(_translate("MainWindow", " Universidad Autonoma De Occidente"))
self.label_6.setText(_translate("MainWindow", "Por: Mario Gomez, Viviana Calero, Sara Chillito, Stefania Calderon"))
self.BotonInicio.setText(_translate("MainWindow", "Inicio del programa"))
self.label_7.setText(_translate("MainWindow", "Edad:"))
self.label_8.setText(_translate("MainWindow", "Genero:"))
self.label_9.setText(_translate("MainWindow", "Patologias:"))
self.BoxGenero.setItemText(0, _translate("MainWindow", "Hombre"))
self.BoxGenero.setItemText(1, _translate("MainWindow", "Mujer"))
self.BotonExportar.setText(_translate("MainWindow", "Exportar datos"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
It is recommended not to modify the code provided by Qt Designer since if you want to modify the design you will have to rewrite your code, besides the Ui_MainWindow class is not a widget but a class that is used to fill some widget, so I recommend deleting the changes .
On the other hand is not suitable to run with a timer with a range of 0, since it does not allow updating the GUI, on the other hand the reading of the serial is blocking, so a possible solution is to use QThread, and use a signal to update the data in the GUI thread.
class Ui_MainWindow(object):
...
class ArduinoThread(QtCore.QThread):
dataChanged = QtCore.pyqtSignal(str)
def __init__(self, *args, **kwargs):
QtCore.QThread.__init__(self, *args, **kwargs)
self.raw = serial.Serial('com4', 9600)
self.raw.close()
self.raw.open()
def run(self):
while True:
datos_array = self.raw.readline().decode().split(',')
if datos_array:
datos = datos_array[0]
self.dataChanged.emit(datos)
QtCore.QThread.msleep(10)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.BotonInicio.clicked.connect(self.start)
self.m = 0
def update_plot(self, dato):
if self.m > 99:
self.y1 = np.zeros(100, dtype=float)
self.m = 0
else:
self.y1[self.m] = dato
self.m += 1
self.curva1.setData(self.y1)
def start(self):
thread = ArduinoThread(self)
thread.dataChanged.connect(self.update_plot)
thread.start()
self.win = pg.GraphicsWindow()
self.win.setWindowTitle('Datos de arduino')
self.p1 = self.win.addPlot()
self.p1.setYRange(0, 1024, padding=0)
self.curva1 = self.p1.plot()
self.y1 = np.zeros(100, dtype=float)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
As you can see, I have made some changes to improve the application as verifications, use variables when necessary, etc.
Although a way compatible with Qt is to use QSerialPort that emits the readyRead signal when there is a new data:
from PyQt5 import QtCore, QtGui, QtWidgets, QtSerialPort
....
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.BotonInicio.clicked.connect(self.start)
def update_plot(self):
while self.ser.canReadLine():
line = bytearray(self.ser.readLine()).decode()
self.processLine(line)
def processLine(self, line):
datos_array = line.strip().split(',')
if datos_array:
dato = datos_array[0]
if self.m > 99:
self.y1 = np.zeros(100, dtype=float)
self.m = 0
else:
self.y1[self.m] = dato
self.m += 1
self.curva1.setData(self.y1)
def start(self):
self.ser = QtSerialPort.QSerialPort("com4", self)
self.ser.setBaudRate(9600)
self.ser.readyRead.connect(self.update_plot)
self.ser.open(QtSerialPort.QSerialPort.ReadOnly)
self.win = pg.GraphicsWindow()
self.win.setWindowTitle('Datos de arduino')
self.p1 = self.win.addPlot()
self.p1.setYRange(0, 1024, padding=0)
self.curva1 = self.p1.plot()
self.y1 = np.zeros(100, dtype=float)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

changing textlabel and qthread pyqt

I write some GUI with qt designer. This script is generated with qt designer and I wrote class mythread:
from PyQt4 import QtCore, QtGui
import time
import socket
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class mythread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
while True:
TCP_IP =
TCP_PORT =
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send('GLST\r\n')
glst = s.recv(1024)
if glst:
s.send('GLSC\r\n')
glsc = s.recv(1024)
print glst,glsc
s.close()
time.sleep(0.1)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 40, 221, 31))
self.label.setStyleSheet(_fromUtf8("background-color:rgb(48, 48, 48)"))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(290, 40, 221, 31))
self.label_2.setStyleSheet(_fromUtf8("background-color:rgb(48, 48, 48)"))
self.label_2.setObjectName(_fromUtf8("label_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.label, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.mythread)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600; color:#ffffff;\">co:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600; color:#ffffff;\">co:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
In class mythread I have function run which is make connection to server and then send two commands on them. This function receives some data(glst,glsc) and now I want to see dynamically this data (glst,glsc) with while loop in textlabel in GUI.
But I dont know how connect thread to gui-textlabel.
Thanks for any ideas. Johny
Here is script with supplement from coments
from PyQt4 import QtCore, QtGui
import time
import socket
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class mythread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
while True:
TCP_IP =
TCP_PORT =
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send('GLSC\r\n')
glsc = s.recv(1024)
print glsc[:9]
self.setLog(glsc[:9])
if glsc:
s.send('GLST\r\n')
glst = s.recv(1024)
print glst
self.setLog(glst)
s.close()
time.sleep(0.1)
def setLog (self, text):
self.emit(SIGNAL("log(QString)"), QtCore.QString(text))
class Ui_MainWindow(object):
def setupUi(self,MainWindow):
self.mythread = mythread()
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 40, 221, 31))
self.label.setStyleSheet(_fromUtf8("background-color:rgb(48, 48, 48)"))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(290, 40, 221, 31))
self.label_2.setStyleSheet(_fromUtf8("background-color:rgb(48, 48, 48)"))
self.label_2.setObjectName(_fromUtf8("label_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.mythread, QtCore.SIGNAL("log(QString)"), self.label.setText)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600; color:#ffffff;\"></span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600; color:#ffffff;\"></span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
LAST EDITED : 12 / 8 / 2014 9 : 01 Refactor code
I suggest you implement by inheritance class QtGui.QMainWindow, please see my refactor code;
from PyQt4 import QtCore, QtGui
import sys
import time
import socket
class QMyThread (QtCore.QThread):
def __init__(self, parent = None):
super(QtCore.QThread, self).__init__(parent)
def run(self):
while True:
TCP_IP = '127.0.0.1'
TCP_PORT = 23
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.connect((TCP_IP, TCP_PORT))
mySocket.send('GLSC\r\n')
glsc = mySocket.recv(1024)
print glsc[:9]
self.setLog1(glsc[:9])
if glsc:
mySocket.send('GLST\r\n')
glst = mySocket.recv(1024)
print glst
self.setLog2(glst)
mySocket.close()
time.sleep(0.1)
def setLog1 (self, text):
self.emit(QtCore.SIGNAL('log1(QString)'), QtCore.QString(text))
def setLog2 (self, text):
self.emit(QtCore.SIGNAL('log2(QString)'), QtCore.QString(text))
class QMyMainWindow (QtGui.QMainWindow):
def __init__(self, parent = None):
super(QtGui.QMainWindow, self).__init__(parent)
self.myInit()
def myInit (self):
self.mythread = QMyThread(self)
self.setObjectName('MainWindow')
self.resize(800, 600)
self.centralwidget = QtGui.QWidget(self)
self.centralwidget.setObjectName('centralwidget')
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 40, 221, 31))
self.label.setStyleSheet('color:rgb(255, 255, 255); background-color:rgb(48, 48, 48);')
self.label.setObjectName('label')
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(290, 40, 221, 31))
self.label_2.setStyleSheet('color:rgb(255, 255, 255); background-color:rgb(48, 48, 48);')
self.label_2.setObjectName('label_2')
self.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(self)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName('menubar')
self.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(self)
self.statusbar.setObjectName('statusbar')
self.setStatusBar(self.statusbar)
self.setWindowTitle('Main Windows')
self.connect(self.mythread, QtCore.SIGNAL('log1(QString)'), self.label.setText)
self.connect(self.mythread, QtCore.SIGNAL('log2(QString)'), self.label_2.setText)
self.mythread.start()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QMyMainWindow()
MainWindow.show()
sys.exit(app.exec_())
LAST EDITED : 12 / 8 / 2014 7 : 34 Fix error 'connect not found'
I same idea to your main widget to QThread, connect it with SIGNAL back to main widget;
Main widget
.
.
def setupUi(self, MainWindow):
.
.
self.mythread = mythread()
.
.
QtCore.QObject.connect(self.mythread, QtCore.SIGNAL("log(QString)"), self.label.setText)
.
.
QThread
class mythread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
while True:
TCP_IP =
TCP_PORT =
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send('GLST\r\n')
glst = s.recv(1024)
self.setLog(glst)
if glst:
s.send('GLSC\r\n')
glsc = s.recv(1024)
self.setLog(glsc)
print glst,glsc
s.close()
time.sleep(0.1)
def setLog (self, text):
self.emit(QtCore.SIGNAL("log(QString)"), QtCore.QString(text))
Regards,

Resources