-1

'Every time the button is pressed, I want the onClicked function to work and increase the x, y, z values, but it always returns 0. How can I solve this problem? Where am I making mistakes?'

global x,y,z
x = 0
y = 0
z = 0
class main2(QMainWindow):
    def __init__(self, x, y, z):
        super(main2, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.x= x
        self.y= y
        self.z= z

        self.ui.btnCount.clicked.connect(self.onClicked)

    def onClicked(self):
        self.x += 1
        self.y += 1
        self.z += 1
        print(x, y, z)
        return x, y, z

def main():
    app = QApplication(sys.argv)
    win = main2(x, y, z)
    win.show()
    sys.exit(app.exec_())
main()    
uckocaman
  • 53
  • 8
  • 2
    Note, `self.x` is not the same as `x`. They're two separate variables. – Carcigenicate Jul 24 '20 at 16:42
  • 2
    `self.x` and `x` are not the same thing – jordanm Jul 24 '20 at 16:42
  • An integer is not something that supports in-place addition, so after `x = 0; self.x = x; self.x += 1`, `x` and `self.x` will be different. (Contrast with e.g. a list: `x = ['foo']; self.x = x; self.x += ['bar']`, `x` and `self.x` would still point to the same list.) – alani Jul 24 '20 at 16:47
  • thank you all. I made a very simple mistake :) – uckocaman Jul 24 '20 at 17:05

1 Answers1

0

Maybe you want to look at self.x/y/z. Here, you never update x/y/z, so they stay at 0...

Adrien Kaczmarek
  • 530
  • 4
  • 13