0

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Bmorg
  • 11
  • 2
  • I'm not sure I'm understanding you. You want a unique function where you can pass a label *and* a variable *name*, so that function would then call `setText` of that label with the *value* of the variable? – musicamante Aug 15 '21 at 15:40
  • Note: 1. Using pyautogui to get the mouse cursor makes that an unnecessary dependency: just use [`QtGui.QCursor.pos()`](https://doc.qt.io/qt-5/qcursor.html#pos) and eventually format the result; 2. Accessing UI elements (like using `setText` on a label) from external threads is highly discouraged as widgets are not thread safe, and it could lead to drawing artifacts or even make the program crash: signals must **always** be used instead; if what you need is just to call a function repeatedly, then use QTimer. – musicamante Aug 15 '21 at 15:47
  • @musicamante thanks for the feedback I'll look into signals and read up on those. I am making a bot that interacts with another window, and can be adjusted and gets feedback on the gui, and was going to use pyautogui for sending some of the inputs. – Bmorg Aug 15 '21 at 17:17
  • ok, got it. Besides that, can you please clarify what I asked in my first comment? – musicamante Aug 15 '21 at 17:35
  • @musicmante yes I was hoping to do pass the label that was being updated and the value to set that label to into a function. But that's because I was trying to do it through the an external thread, which since it is unsafe I will use your recommendations and the Thread method from the linked post – Bmorg Aug 15 '21 at 19:42

0 Answers0