Python Forum

Full Version: How to update the PyQt GUI?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey folks,

I just started with python and PyQt and hope that you can help me with my problem.

I created a GUI.ui with the QtDesigner, converted it to a GUI.py file and imported it into my app.py
What I want is to change the text of the "testlabel" to the currently chosen entry of the combobox "comboPortList"
I need to update the GUI, but I'm somehow stucked... Huh

I have the following folder structure:

main.py
package:
->__init__.py
->app.py
->ComPorts.py
->GUI.py

Here is my code:
main.py:
if __name__ == '__main__':

    import sys
    from package import app

    sys.exit(app.run())
ComPorts.py:
def listPorts():

    import serial.tools.list_ports

    ports = list(serial.tools.list_ports.comports())

    resultPorts = []
    descriptionPorts = []

    for p in ports:
        resultPorts.append(p.device)
        descriptionPorts.append(str(p.description))

    return (descriptionPorts)
GUI.py:
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'GUI.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(740, 603)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gridLayout_3 = QtGui.QGridLayout(self.centralwidget)
        self.gridLayout_3.setObjectName(_fromUtf8("gridLayout_3"))
        self.gridLayout_2 = QtGui.QGridLayout()
        self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
        self.comboPortList = QtGui.QComboBox(self.centralwidget)
        self.comboPortList.setObjectName(_fromUtf8("comboPortList"))
        self.gridLayout_2.addWidget(self.comboPortList, 0, 1, 1, 1)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.gridLayout_2.addLayout(self.horizontalLayout, 0, 0, 1, 1)
        self.testlabel = QtGui.QLabel(self.centralwidget)
        self.testlabel.setObjectName(_fromUtf8("testlabel"))
        self.gridLayout_2.addWidget(self.testlabel, 2, 1, 1, 1)
        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.gridLayout_2.addItem(spacerItem, 1, 1, 1, 1)
        self.gridLayout_3.addLayout(self.gridLayout_2, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 740, 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.comboPortList, QtCore.SIGNAL(_fromUtf8("editTextChanged(QString)")), self.testlabel.setText)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "My GUI", None))
        self.testlabel.setText(_translate("MainWindow", "TextLabel", None))


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_())
app.py:
import sys
from PyQt4 import QtCore, QtGui
from package.GUI import Ui_MainWindow
from package.ComPorts import listPorts

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

def fillComPorts(qtWnd, portlist):
        qtWnd.comboPortList.addItems(portlist)

def run():
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    ports = listPorts()
    fillComPorts(window, ports)
    window.show()
    return app.exec_()

run()
Thank you in advance for your help! Smile

kally