How do I update button when a string is printed?



  • Hi

    When this is executed "print("Hello World! Im a laser")" in laser.py I want the "Turn on" button to change to "ON". How would this be done? Run GUI.py to test and laser.py is the file to import.

    GUI.py

    import paho.mqtt.client as mqtt
    import os
    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5 import QtWidgets, uic
    from mqtt import *
    import json
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self,parent = None):
            QMainWindow.__init__(self)
            super(MainWindow, self).__init__(parent)
            self.mdi = QMdiArea()
            self.setCentralWidget(self.mdi)
    
            self.setMinimumSize(QSize(800, 600))
            self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com")
    
            pybutton = QPushButton('Add device', self)
    
            pybutton.clicked.connect(self.importbutton)
    
            pybutton.move(100, 400)
            pybutton.resize(150, 32)
    
            self.textbox = QLineEdit(self)
            self.textbox.move(100,350)
            self.textbox.resize(100, 32)
            
            self.fileName_UI = ""
    
        def importbutton(self):
            self.fileName_UI = self.textbox.text()
            self.loadGUI()
    
        def getGUIFilename(self):
            return self.fileName_UI
    
        def loadGUI(self):
            print("Searching file", self.fileName_UI)       
            try:
                module = __import__(self.fileName_UI)
                my_class = getattr(module, "SubWindow")
    
                sub = QMdiSubWindow()
    
                sub.setWidget(my_class())
                sub.setWindowTitle("New GUI:  " + self.fileName_UI)
                self.mdi.addSubWindow(sub)
                sub.show()
    
                print("creating new instance " + self.fileName_UI)
                client = device("Device")
                client.run()
    
                client.loop_start()  # start the loop
                device_message = self.fileName_UI
                time.sleep(2)
                print("Subscribing to topic", "microscope/light_sheet_microscope/UI")
                client.subscribe("microscope/light_sheet_microscope/UI")
                print("Publishing message to topic", "microscope/light_sheet_microscope/UI")
                client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": "adding device"}}, indent=2))
                time.sleep(1)  # wait
                client.loop_stop()  # stop the loop
                print("Device added" + "\n")
    
                client.loop_start()
                time.sleep(2)
                print("Subscribing to topic", "microscope/light_sheet_microscope/UI/list_of_devices")
                client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices")
                print("Publishing message to topic", "microscope/light_sheet_microscope/UI/list_of_devices")
                client.publish("microscope/light_sheet_microscope/UI/list_of_devices", self.fileName_UI + " added to device list")
                time.sleep(1)
                client.loop_stop()
    
                listofdevices = []
                listofdevices.append(self.fileName_UI)
                with open("list_of_devices.txt", "a+") as myfile:
                    for item in listofdevices:
                        myfile.write(item + " ")
                print(item + " added to device list")
    
                def readFile(fname):
                	try:
                		with open(fname, "r") as f:
                			for item in f:
                				print("List of devices added: " + item + " ")
                	except:
                		print("No devices added yet")
                readFile("list_of_devices.txt")
            except:
                print("creating new instance " + self.fileName_UI)
                client = device("Device")
                client.run()
    
                client.loop_start()  # start the loop
                device_message = self.fileName_UI
                time.sleep(2)
                print("Subscribing to topic", "microscope/light_sheet_microscope/UI")
                client.subscribe("microscope/light_sheet_microscope/UI")
                print("Publishing message to topic", "microscope/light_sheet_microscope/UI")
                client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2))
                time.sleep(2)  # wait
                client.loop_stop()  # stop the loop
                print(device_message + ".py " + "file doesn't exist")
                print("Device not added")
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        mainWin = MainWindow()
        mainWin.show()
        try:
            mainWin.show()
            os.remove("list_of_devices.txt")
            print("Awaiting devices to be launched")
        except:
            print("Awaiting devices to be launched")
        publishedMessage = mainWin.getGUIFilename()
        sys.exit(app.exec_())
    

    laser.py

    from PyQt5 import QtCore, QtGui, QtWidgets
    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5 import QtWidgets, uic
    from mqtt import *
    import json
    
    class SubWindow(QWidget):
        def __init__(self, parent = None):
            super(SubWindow, self).__init__(parent)
            self.setMinimumSize(QSize(300, 200))
            label = QLabel("Laser",  self)
            
            self.Button = QtWidgets.QPushButton("Turn on",self)        
            self.Button.clicked.connect(self.on)        
            self.Button.setGeometry(QtCore.QRect(10, 40, 81, 23))
            self.Button2 = QtWidgets.QPushButton("Status", self)
            self.Button2.clicked.connect(self.status)
            self.Button2.setGeometry(QtCore.QRect(20, 40, 81, 23))
            self.Button2.move(220,10)
    
        def status(self):
            client = device("Laser")
            client.run()
    
            client.loop_start()
            default_status = ("Uninitialised")
            time.sleep(2)
            print("Subscribing to topic", "microscope/light_sheet_microscope/UI")        
            client.subscribe("microscope/light_sheet_microscope/UI")
            print("Publishing message to topic", "microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": "laser", "status": default_status}}))  
            print("Current status = " + default_status)
            client.loop_stop()  # stop the loop
    
        def on(self):
            client = device("Laser")
            client.run()
    
            client.loop_start()
            uninitialised_status = "Uninitialised"
            time.sleep(2)
            print("Subscribing to topic", "microscope/light_sheet_microscope/UI")        
            client.subscribe("microscope/light_sheet_microscope/UI")
            print("Publishing message to topic", "microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": "laser", "status": uninitialised_status}}))  
            print("Current status = " + uninitialised_status)
            client.loop_stop()  # stop the loop
    
            client.loop_start()
            initialising_status = "Initialising"
            time.sleep(2)
            print("Subscribing to topic", "microscope/light_sheet_microscope/UI")        
            client.subscribe("microscope/light_sheet_microscope/UI")
            print("Publishing message to topic", "microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": "laser", "status": initialising_status}}))  
            print("Current status = " + initialising_status)
            client.loop_stop()  # stop the loop
    
            client.loop_start()
            initialised_status = "Initialised"
            time.sleep(2)
            print("Subscribing to topic", "microscope/light_sheet_microscope/UI")        
            client.subscribe("microscope/light_sheet_microscope/UI")
            print("Publishing message to topic", "microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": "laser", "status": initialised_status}}))  
            print("Current status = " + initialised_status)
            client.loop_stop()  # stop the loop
    
            client.loop_start()
            configuring_status = "Configuring"
            time.sleep(2)
            print("Subscribing to topic", "microscope/light_sheet_microscope/UI")        
            client.subscribe("microscope/light_sheet_microscope/UI")
            print("Publishing message to topic", "microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": "laser", "status": configuring_status}}))  
            print("Current status = " + configuring_status)
            client.loop_stop()  # stop the loop
    
            client.loop_start()
            configured_status = "Configured"
            time.sleep(2)
            print("Subscribing to topic", "microscope/light_sheet_microscope/UI")        
            client.subscribe("microscope/light_sheet_microscope/UI")
            print("Publishing message to topic", "microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": "laser", "status": configured_status}}))  
            print("Current status = " + configured_status)
            client.loop_stop()  # stop the loop
    
            client.loop_start()
            run_status = "run"
            time.sleep(2)
            print("Subscribing to topic", "microscope/light_sheet_microscope/UI")        
            client.subscribe("microscope/light_sheet_microscope/UI")
            print("Publishing message to topic", "microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": "laser", "status": run_status}}))  
            print("Current status = " + run_status)
            client.loop_stop()  # stop the loop
    
            print("Hello World! Im a laser")
    
        def closeEvent(self, event):
            self.close()
    

    Thanks



  • Well then you set it? Basically right after your

    print("Hello World! Im a laser")
    

    you would want to put the following:

    self.Button.setText('On')
    

 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.