Problem:
I'm trying to write my first application with GUI in Python. I split my program to two files: one with GUI (GUI.py) and second one with program logic (Test.py).
I would like to change something in my GUI during program execution (for example QLabel text with status) from Test.py level.
I don't know how to get access to any controls.
Code:
GUI.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QLabel, QPushButton, QGridLayout
class Ui_Widget(object):
def setupUi(self):
# controls
statusLbl = QLabel("Status", self)
changeBtn = QPushButton("&Change text", self)
closeBtn = QPushButton("&Close", self)
#statusLbl.setText("working") # <- here it works, but not in Test.py
# GridLayout
CtrLayout = QGridLayout()
CtrLayout.addWidget(statusLbl, 0, 0)
CtrLayout.addWidget(changeBtn, 0, 1)
CtrLayout.addWidget(closeBtn, 0, 2)
self.setLayout(CtrLayout)
# onClick events
changeBtn.clicked.connect(self.changeText)
closeBtn.clicked.connect(self.closeApp)
#self.setGeometry(20, 20, 300, 100)
#self.setWindowTitle("TEST APP")
self.show()
Test.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication, QWidget
from GUI import Ui_Widget
class TestApp(QWidget, Ui_Widget):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi()
def changeText(self):
generateReport(self) # code moved to separate function for better clarity
def closeApp(self):
self.close()
def generateReport(obj):
statusLbl.setText("working") # <- change of statusLbl.setText is not working
# ...
# 200 lines of code here
#...
statusLbl.setText("not " + statusLbl.text() )
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
WND = TestApp()
sys.exit(app.exec_())