Eilat Eilat - 1 month ago 14
Python Question

How to override a variable from another class in PyQt5?

Im working on a PyQt5 app:

I have two classes (mainWindow & devicePanel).
Got some buttons (qbtn) in mainWindow, which makes devicePanel to appear when they are clicked.
My problem is that I need to override some variables in devicePanel class on 'qbtn' click event.

So:

When mainWindow.qbtn is pressed, it should make devicePanel appear and override some of its variables (l_ip, l_st, etc).

I CANNOT make mainWindow parent of devicePanel, please don't suggest it, it won't solve my problem.

Here is my code:

class mainWindow(QWidget):

def __init__(self, parent=None):

super(mainWindow, self).__init__(parent)
self.initUI()


def initUI(self):

self.resize(1280, 800)
self.setWindowTitle('Test window')
self.genBtns()
self.show()


def genBtns(self):

devices = []
devices.append(['A', 21, 1, 'W', '2', 'n/a'])
devices.append(['A', 22, 2, 'O', '0', 'n/a'])
devices.append(['A', 31, 3, 'W', '32', '11'])

for a in range (0, int(len(devices))):
self.addButton(devices[a][0], devices[a][1], devices[a][2], devices[a][3], devices[a][4], devices[a][5])


def addButton(self, z, x, y, status, bp, cp):

qbtn = QPushButton(str(x), self)
qbtn.clicked.connect(lambda: self.buttonClicked(z, x, y, status, bp, cp)) # BUTTON CLICK EVENT
qbtn.resize(70, 70)
qbtn.move(20, 20)
if (status == 'O'):
qbtn.setStyleSheet("background-color: red")
elif (status == 'W'):
qbtn.setStyleSheet("background-color: blue")
else:
qbtn.setStyleSheet("background-color: gray")


def buttonClicked(self, z, x, y, status, bp, cp): # BUTTON CLICK METHOD

self.dp = devicePanel()
self.dp.initUI()
self.dp.l_ip = '192.168.0.134'
self.dp.show()


class devicePanel(QWidget):

l_ip = '192.168.0.135' #NEED TO CHANGE THESE VALUES!
l_st = 'active'
l_bp = '10'
l_cp = '2'
l_bc = '90'
l_cc = '150'
l_sc = '240'


def __init__(self):

super(devicePanel, self).__init__(None)
self.addDPbtns()
self.initUI()

Answer

Update your class so it accepts arguments to set those values. In this example, the l_xx class variables just become the defaults. The self.xx variables are the real variables. They will be set to whatever you pass in, or if you don't pass anything in, they become defaults.

class devicePanel(QWidget):

    l_ip = '192.168.0.135'    #NEED TO CHANGE THESE VALUES!
    l_st = 'active'
    l_bp = '10'
    l_cp = '2'
    l_bc = '90'
    l_cc = '150'
    l_sc = '240'

    def __init__(ip=None, st=None, bp=None, cp=None, bc=None, cc=None, sc=None):
        super(devicePanel, self).__init__()
        self.ip = ip or self.l_ip
        self.st = st or self.l_st
        self.bp = bp or self.l_bp
        self.cp = cp or self.l_cp
        self.bc = bc or self.l_bc
        self.cc = cc or self.l_cc

Then instantiate it like ths

def buttonClicked(self, z, x, y, status, bp, cp): # BUTTON CLICK METHOD

    ip = '192.168.0.134'
    self.dp = devicePanel(ip=ip, bp=bp, cp=cp, st=status)
    self.dp.initUI()
    self.dp.show()