I'm making a rather large GUI and will have lots of things to change. I want to make a definition for updating labels. Here's my current working program:
I am using qtdesigner, and PySide2.
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.setWindowTitle("TestGui Window")
def GetMouseLoc(self):
MouseLoc = pyautogui.position()
self.MouseLocLabel.setText(str(MouseLoc))
class DataHandler(Thread):
def __init__(self):
Thread.__init__(self)
self.daemon = True
self.start()
def run(self):
global mainWindow
while True:
mainWindow.GetMouseLoc()
time.sleep(0.1)
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
DataHandler()
sys.exit(app.exec_())
while True:
time.sleep(10000)
I could get it working doing this but I don't want to make several different definitions to call for each area that gets updated. I am wondering if it is possible to do something closer to this where I have a definition "SetLabelTest" that I can call and send a label name and a value to use. This way I only need a few definitions and can pass in which one to be updated.
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.setWindowTitle("TestGui Window")
def SetLabelTest(LabelName, LabelText):
self.LabelName.setText(LabelText)
class DataHandler(Thread):
def __init__(self):
Thread.__init__(self)
self.daemon = True
self.start()
def run(self):
global MouseLoc
global mainWindow
while True:
MouseLoc = pyautogui.position()
mainWindow.SetLabelTest('MouseLocLabel', str(MouseLoc))
time.sleep(0.1)
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
DataHandler()
sys.exit(app.exec_())
while True:
time.sleep(10000)
However I have been unable to get anything to work where I can use .setText with a variable used for the labelname.
This would make the code shorter, easier to read and I can handle more of the information outside of the MainWindow and just pass information through.
With how this is arranged most of my main body of code will be written in a class such as DataHandler.