PyQt5 GUI How To Create QButtonGroup in PyQt5

In this PyQt5 GUI article i want to show How To Create QButtonGroup in PyQt5. QButtonGroup class provides a container to organize groups of button widgets. QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container (see QGroupBox for a container widget), but instead manages the states of each of the buttons in the group.

 

 

What is QButtonGroup in PyQt5 ?

In PyQt5 QButtonGroup is a container that groups together a set of buttons such as QRadioButton or QPushButton, so that they behave as a single entity. QButtonGroup class provides convenient way to manage and manipulate a set of buttons as a group.

with QButtonGroup, you can specify that only one button in the group can be checked at any given time, so when a button is checked, all other buttons in the group are automatically unchecked. You can also get the checked button in the group, and iterate over all the buttons in the group.

 

 

Join PyQt5 Full Course for Free

PyQt5 Course

 

 

 

Also you can read more Python GUI articles in the below links

 

 

 

PyQt5 GUI How To Create QButtonGroup in PyQt5

First we need some imports.

 

 

 

After that we are going to create our main Window class that extends from QWidget. and in the constructor of the class we need to add some requirements of the window like set window title, window icon and window geometry.

 

 

 

So in here we have created an QHBoxLayout with QLabel, and we have added the label to the hbox layout. also we have set the font for the label.

 

 

In here we have created the object for our QButtonGroup.

 

 

 

Now we have connected the buttonClicked signal of QButtonGroup to button_clicked() method that we will make later.

 

 

 

This is for creating of QPushButton, we have created some buttons and we have added that to the buttongroup, also we have used some font styles for our buttons.

 

 

 

This is the method that we have connected with the buttongroup signal at the top.

 

 

 

Also every PyQt5 application must create an application object.

 

 

 

Finally, we enter the mainloop of the application. The event handling starts from this point. 

 

 

 

 

Run the complete code and this will be the result

PyQt5 GUI How To Create QButtonGroup
PyQt5 GUI How To Create QButtonGroup

 

 

 

Complete source code for PyQt5 GUI How To Create QButtonGroup

 

 

 

 

 

 

 

Also you can watch the complete video for this article

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×