How to remove space between radiobuttons in pyqt5

Viewed 100

enter image description here

How To Remove Padding Between Male And Female RadioButtons? Here's My Code :

def UiComponents(self):
        self.gridbox=QGridLayout()
        self.label=QLabel("What's Your Gender?")
        self.r1=QRadioButton("Male")
        self.r2=QRadioButton("Female")
        self.r3=QRadioButton("Rather Not To Say")
        self.button=QPushButton("Submit")
        self.gridbox.addWidget(self.label,0,0)
        self.gridbox.addWidget(self.r1,1,0)
        self.gridbox.addWidget(self.r2,1,1)
        self.gridbox.addWidget(self.r3,1,2)
        self.gridbox.addWidget(self.button,2,0)
        self.setLayout(self.gridbox)
        self.show()
2 Answers

Try changing spacing attribute at setStyleSheet() method like so

r1.setStyleSheet("""
       QRadioButton {
          spacing : 20px; #(<- example value)
       }
""")

QGridLayout works like html table - you got columns and rows, column width is equal to wider widget in column, in your case it's label. To avoid stretching first column, span label across all columns using columnSpan argument of addWidget (and button too).

self.gridbox.addWidget(self.label,0,0,1,3)
...
self.gridbox.addWidget(self.button,2,0,1,3)

I would recomend using QtDesigner to create ui, it saves a lot of time and effort and also wysiwyg.

Related