I'm trying to make a new 'Qradiobutton' when I click on create button

75 views Asked by At

I'm trying to make a new 'Qradiobutton' when I click on create button... but the button creates just one Qradiobutton. any advice ???

my code:

import sys
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QWidget, QPushButton, QRadioButton
)

class main(QMainWindow):
    def __init__(self):
        super().__init__()

        self.newUi()

        self.setGeometry(400, 400, 500, 500)
        self.setWindowTitle('Create')
        self.show()

        self.radio1 = QRadioButton('first', self)
        self.radio1.setGeometry(50, 50, 60, 20)

        self.radio2 = QRadioButton('second', self)
        self.radio2.setGeometry(50, 90, 60, 20)

        self.radio3 = QRadioButton('third', self)
        self.radio3.setGeometry(50, 130, 60, 20)

    def newUi(self):
        self.btn = QPushButton('create', self)

        self.btn.clicked.connect(self.create)


    def create(self):
        n = 1

        if n == 1:
            self.radio1.show()
            n = 2

        elif n == 2:
            self.radio2.show()
            n == 3

        elif n == 3:
            self.radio3.show()

        else:
            print('done')



if __name__ == "__main__":
    app = QApplication(sys.argv)
    e = main()
    app.exec()
2

There are 2 answers

1
eyllanesc On BEST ANSWER

You are not creating a QRadioButton, you are making the existing radiobutton visible. In your case the problem is that n is always 1 so it will only make visible the radiobutton associated with n = 1, in each click you must increase the value of n.

Note: in your case "n" is a local variable that will be destroyed instantly, so each click n is 1.

def newUi(self):
    self.btn = QPushButton('create', self)
    self.n = 0
    self.btn.clicked.connect(self.create)


def create(self):
    self.n +=  1
    if self.n == 1:
        self.radio1.adjustSize()
        self.radio1.show()
        

    elif self.n == 2:
        self.radio2.adjustSize()
        self.radio2.show()

    elif self.n == 3:
        self.radio3.adjustSize()
        self.radio3.show()

    else:
        print('done')
0
musicamante On

The first issue in your code is that you're declaring the n variable as 1 at the beginning of the function. No matter what you do after that, everytime you call that create function, you're always setting n as 1, so your other elif/else statements will never get processed.

Then, even assuming you didn't do the above, what you're declaring there is called a local variable: it is a variable that exists only within the create function, and gets completely deleted from memory as soon as that function returns.

The solution is to make that variable persistent, ensure that it doesn't get deleted and is kept alive "as long as possible".
A common attempt to work around that is to use global variables (the statement global n at the very beginning of the function). That is also a common mistake, and should never be used for such reasons. Global variables are not intended for that usage, and using them usually leads to misunderstanding and unexpected behavior that creates "silent bugs" which are very difficult to track.

The correct solution is to create an instance attribute, which is a variable that is a member of the instance.

In order to do that, you must declare that attribute before checking or changing it, and that's normally done within the __init__ of the class. Then, you check that value in the create function and do whatever you need with it.

class main(QMainWindow):
    def __init__(self):
        super().__init__()
        self.n = 1

        self.newUi()
        # ...

    def create(self):
        if self.n == 1:
            self.radio1.show()
            self.n = 2

        elif self.n == 2:
            self.radio2.show()
            self.n = 3

        elif self.n == 3:
            self.radio3.show()
            self.n = 4

        else:
            print('done')