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()
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')