0

I'm making a GUI with pyqt5 and have qlcdnumber to display a variable which I retrieve from an Android app through a socket server. The problem is I can't find any method to display the variable that I have in the qlcdnumber.

Here is the UI code, I tried to use signal but didn't work.

class MyTCPHandler(socketserver.StreamRequestHandler):
    def handle(self):
        while True:
            global angle, strength ,speed, a
            if(not enabled):
                # self.rfile is a file-like object created by the handler;
                # we can now use e.g. readline() instead of raw recv() calls
                self.data = self.rfile.readline().strip()
                if(a==0):
                    print("{} wrote:".format(self.client_address[0]))
                    a+=1
                data = self.data.decode("utf-8").rstrip().strip("\n")
                if(data!= ""):
                    if(data == "disconnect"):
                        print("restarting")
                        self.close()
                        sleep(1)
                        mainServer()
                    elif(data == "shutdown"):
                        p = subprocess.Popen("sudo shutdown -h now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                        print(p)
                    else:    
                        temp = data.split(":")
                        angle = int(temp[0])
                        strength = int(temp[1])
                        speed = float(temp[2])


class Ui_AppWindow(QtWidgets.QWidget):

    def __init__(self):
        super(Ui_AppWindow, self).__init__()
        self.setStyleSheet('background-color:#efefef; color:white;')
        global add, angle, speed

        self.layout = QtWidgets.QVBoxLayout()

        self.label = QtWidgets.QLabel("\t\t"+add)
        self.label.setStyleSheet("color: #005C99;")
        self.layout.addWidget(self.label)

        palette = QtGui.QPalette()
        palette.setColor(palette.WindowText, QtGui.QColor(85, 85, 255))
        palette.setColor(palette.Background, QtGui.QColor(0, 170, 255))
        palette.setColor(palette.Light, QtGui.QColor(0, 92, 153))
        palette.setColor(palette.Dark, QtGui.QColor(0, 92, 137))

        self.speed = QtWidgets.QLCDNumber(self)
        self.speed.setGeometry(QtCore.QRect(100, 100, 64, 23))
        self.speed.setObjectName("speed")
        self.speed.display(speed)
        self.speed.setPalette(palette)

        self.angle = QtWidgets.QLCDNumber(self)
        self.angle.setGeometry(QtCore.QRect(200, 100, 64, 23))
        self.angle.setObjectName("angle")
        self.angle.display(angle)
        self.angle.setPalette(palette)

        self.speed_lable = QtWidgets.QLabel("Speed")
        self.speed_lable.setGeometry(QtCore.QRect(100, 70, 47, 25))
        self.speed_lable.setObjectName("speed_lable")
        self.speed_lable.setStyleSheet('color:#00497a;')

        self.angle_lable = QtWidgets.QLabel("Angle")
        self.angle_lable.setGeometry(QtCore.QRect(200, 70, 47, 25))
        self.angle_lable.setObjectName("angle_lable")
        self.angle_lable.setStyleSheet('color:#00497a;')

        self.hbl = QtWidgets.QHBoxLayout()
        self.hbl.addWidget(self.speed_lable)
        self.hbl.addWidget(self.angle_lable)
        self.layout.addLayout(self.hbl)

        self.hb = QtWidgets.QHBoxLayout()
        self.hb.addWidget(self.speed)
        self.hb.addWidget(self.angle)
        self.layout.addLayout(self.hb)

        self.setWindowTitle("RasControl GUI: Android Control")
        self.setLayout(self.layout)
        self.setGeometry(100, 100, 400, 200)


    def setAngle(self):
        global angle
        self.angle.display(angle)        

    def setSpeed(self):
        global speed
        self.speed.display(speed)

and I have variables that I want to set to be displayed in the qlcdnumber;

global speed, angle
server = socketserver.TCPServer((ip, port), MyTCPHandler)
server.serve_forever()

and inside the main GUI im calling the Ui_AppWindow gui, but i need the qlcdnumber to be updated with the value of the global variable speed and angle each time they are changed.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
medyas
  • 1,166
  • 13
  • 21
  • Your code is strange, I see the GUI but I do not see where you get the data from, like speed and angle. – eyllanesc Nov 19 '17 at 22:23
  • i got them from android app through a socket server and i set them to the global variable, i have edited and added the code – medyas Nov 20 '17 at 15:47
  • The global variables do not notify the changes to any method so you do not get anything, if you publish a code that can be reproduced I can help you. – eyllanesc Nov 20 '17 at 15:49
  • when i do : variable = Ui_AppWindow() variable.show() , how to update the qlcdnumber when i have changed the value of the speed and angle? – medyas Nov 20 '17 at 15:58
  • i don't know how to do that – medyas Nov 20 '17 at 16:04
  • I just read that he is a member of the StreamRequestHandler class – eyllanesc Nov 20 '17 at 16:06
  • What is `enabled`? – eyllanesc Nov 20 '17 at 16:07
  • StreamRequestHandler is the server class, i'm only setting the data to the global variables there. It's just when i start the server i'm changing the value of both of the variable speed and angle(it's in while true loop) but i can't update the ui (qlcdnumber) to the newest value each time there was a change – medyas Nov 20 '17 at 16:14
  • Test my answer and if it worked, do not forget to mark it as correct. If you do not know how to do it, check the following link: https://stackoverflow.com/tour – eyllanesc Nov 20 '17 at 16:53

1 Answers1

0

The easiest way to do this would just to make functions that are always used to set the values of the global variables in your program, like so:

def set_speed(val):
    global speed
    speed = val
    # ... whatever other logic you'd like to perform

def set_angle(val):
    # ...
C. Feenstra
  • 593
  • 3
  • 11
  • i don't need setters, i need to detect value change in each variable and them update the qlcdnumber with the new value – medyas Nov 20 '17 at 15:57