{"id":4991,"date":"2022-09-21T03:01:01","date_gmt":"2022-09-21T03:01:01","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4991"},"modified":"2022-10-29T07:51:33","modified_gmt":"2022-10-29T07:51:33","slug":"pyqt-qradiobutton","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qradiobutton\/","title":{"rendered":"PyQt QRadioButton"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PyQt <code>QRadioButton<\/code> class to create radio buttons.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qradiobutton'>Introduction to the PyQt QRadioButton <a href=\"#introduction-to-the-pyqt-qradiobutton\" class=\"anchor\" id=\"introduction-to-the-pyqt-qradiobutton\" title=\"Anchor for Introduction to the PyQt QRadioButton\">#<\/a><\/h2>\n\n\n\n<p>The <code>QRadioButton<\/code> class allows you to create a radio button with a label:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">QRadioButton(text&#91;, parent=<span class=\"hljs-literal\">None<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>A radio button has two states: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>on (checked)<\/li><li>off (unchecked)<\/li><\/ul>\n\n\n\n<p>Typically, you use radio buttons in a group. A radio button group provides you with one of many choices.<\/p>\n\n\n\n<p>In a radio button group, you can check only one radio button at a time. If you check another radio button, the previously checked button is unchecked.<\/p>\n\n\n\n<p>By default, radio buttons are auto-exclusive. Also, radio buttons that belong to the same parent widget, are auto-exclusive.<\/p>\n\n\n\n<p>If you want to create multiple exclusive radio buttons, you can group them into multiple <code>QButtonGroup<\/code> widgets.<\/p>\n\n\n\n<p>A radio button emits the <code>toggled()<\/code> <a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-signals-slots\/\">signal<\/a> when it is switched on or off. <\/p>\n\n\n\n<p>If you want to trigger an action when the state of the radio button changes, you can connect a slot to the <code>toggled()<\/code> signal.<\/p>\n\n\n\n<p>Inside the slot, you can use <code>isChecked()<\/code> method to check whether the radio button is switched on or off.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='pyqt-qradiobutton-example'>PyQt QRadioButton example <a href=\"#pyqt-qradiobutton-example\" class=\"anchor\" id=\"pyqt-qradiobutton-example\" title=\"Anchor for PyQt QRadioButton example\">#<\/a><\/h2>\n\n\n\n<p>The following program illustrates how to create a radio button group:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QRadioButton, QLabel, QVBoxLayout\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> Qt\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QWidget)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, *args, **kwargs)<\/span>:<\/span>\n        super().__init__(*args, **kwargs)\n\n        self.setWindowTitle(<span class=\"hljs-string\">'PyQt QRadioButton'<\/span>)\n        self.setMinimumWidth(<span class=\"hljs-number\">300<\/span>)\n\n        <span class=\"hljs-comment\"># create a grid layout<\/span>\n        layout = QVBoxLayout()\n        self.setLayout(layout)\n\n        label = QLabel(<span class=\"hljs-string\">'Please select a platform:'<\/span>, self)\n\n        rb_android = QRadioButton(<span class=\"hljs-string\">'Android'<\/span>, self)\n        rb_android.toggled.connect(self.update)\n\n        rb_ios = QRadioButton(<span class=\"hljs-string\">'iOS'<\/span>, self)\n        rb_ios.toggled.connect(self.update)\n\n        rb_windows = QRadioButton(<span class=\"hljs-string\">'Windows'<\/span>, self)\n        rb_windows.toggled.connect(self.update)\n\n        self.result_label = QLabel(<span class=\"hljs-string\">''<\/span>, self)\n\n        layout.addWidget(label)\n        layout.addWidget(rb_android)\n        layout.addWidget(rb_ios)\n        layout.addWidget(rb_windows)\n        layout.addWidget(self.result_label)\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">update<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-comment\"># get the radio button the send the signal<\/span>\n        rb = self.sender()\n\n        <span class=\"hljs-comment\"># check if the radio button is checked<\/span>\n        <span class=\"hljs-keyword\">if<\/span> rb.isChecked():\n            self.result_label.setText(<span class=\"hljs-string\">f'You selected <span class=\"hljs-subst\">{rb.text()}<\/span>'<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    sys.exit(app.exec())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"452\" height=\"253\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QRadioButton.png\" alt=\"\" class=\"wp-image-4995\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QRadioButton.png 452w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QRadioButton-300x168.png 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create three radio buttons and connect the update method to the <code>toggled()<\/code> signal of each button:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">rb_android = QRadioButton(<span class=\"hljs-string\">'Android'<\/span>, self)\nrb_android.toggled.connect(self.update)\n\nrb_ios = QRadioButton(<span class=\"hljs-string\">'iOS'<\/span>, self)\nrb_ios.toggled.connect(self.update)\n\nrb_windows = QRadioButton(<span class=\"hljs-string\">'Windows'<\/span>, self)\nrb_windows.toggled.connect(self.update)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create the result label that will display which radio button is checked:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.result_label = QLabel(<span class=\"hljs-string\">''<\/span>, self)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because we need to reference the <code>result_label<\/code> in another method, we make it an attribute of the class.<\/p>\n\n\n\n<p>Third, define the <code>update()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">update<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># get the radio button the send the signal<\/span>\n    rb = self.sender()\n\n    <span class=\"hljs-comment\"># check if the radio button is checked<\/span>\n    <span class=\"hljs-keyword\">if<\/span> rb.isChecked():\n        self.result_label.setText(<span class=\"hljs-string\">f'You selected <span class=\"hljs-subst\">{rb.text()}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>update()<\/code> method:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Find the radio button that sent the toggled signal.<\/li><li>Check if the radio button is checked by calling the <code>isChecked()<\/code> method. <\/li><li>Update the result label. To get the text label of the radio button, we use the <code>text()<\/code> method.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the PyQt <code>QRadioButton<\/code> class to create a radio button.<\/li><li>Radio buttons that belong to the same parent belong to an auto-exclusive group.<\/li><li>Connect to the <code>toggled()<\/code> signal to trigger an action when the radio button is switched on or off.<\/li><li>Use the <code>isChecked()<\/code> method to see if the radio button is switched on.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4991\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qradiobutton\/\"\n\t\t\t\tdata-post-title=\"PyQt QRadioButton\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4991\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qradiobutton\/\"\n\t\t\t\tdata-post-title=\"PyQt QRadioButton\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PyQt QRadioButton class to create radio buttons.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4991","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4991"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4991\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4862"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}