如何更新PyQt GUI?

时间:2019-03-13 15:20:10

标签: python user-interface pyqt

我刚开始使用python和PyQt,希望您能为我解决问题。

我使用QtDesigner创建了一个GUI.ui,将其转换为GUI.py文件,并将其导入到我的app.py中。 我想要的是将“ testlabel”的文本更改为组合框“ comboPortList”的当前选定条目 我需要更新GUI,但还是有点卡住了……

我具有以下文件夹结构:

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

这是我的代码: 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()

0 个答案:

没有答案