Python Forum
[closed] "checked" variable (attribute?) origin?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[closed] "checked" variable (attribute?) origin?
#5
QPushButton has an isClhecked() method that can be used to check the checked state of the button. This is available even if you don't call button.setCheckable(True). setCheckable(True) enables toggle behavior for the button but does not add any attributes/variables. When checkable, the button's isChecked() state toggles between True and False when the button is pressed, and the button calls functions attached to the "toggle" signal.

You can see this if you run the example below.
from PySide6.QtWidgets import (QApplication, QMainWindow, QPushButton)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.counter = 0
        self.toggle = False
        self.button = QPushButton("Not Checkable")
        self.button.setCheckable(self.toggle)
        self.button .clicked.connect(self.clicked_cb)
        self.button .clicked.connect(self.clicked_with_toggle_cb)
        self.button.toggled.connect(self.toggled_cb)

        # Set the central widget of the Window.
        self.setCentralWidget(self.button)

    def clicked_cb(self):
        self.counter += 1
        if self.counter >= 3:
            self.counter = 0
            self.toggle = not self.toggle
            self.button.setCheckable(self.toggle)
            self.button.setText("Checkable" if self.toggle else "Not Checkable")

    def clicked_with_toggle_cb(self, checked):
        print("Clicked with toggle state", checked)

    def toggled_cb(self, checked):
        print("Toggled", checked)


app = QApplication()
window = MainWindow()
window.show()
app.exec()
This program toggles the toggle state after three presses of the button. You can see that the clicked_with_toggle_cb is always called when the button is pressed, but the isChecked() state is always False when the button is not checkable. Also notice that the toggled_cb is only called thwne the button is checkable.
Reply


Messages In This Thread
RE: [closed] "checked" variable (attribute?) origin? - by deanhystad - Mar-05-2024, 04:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter checkbutton if checked MC2020 2 6,062 Jan-21-2020, 07:08 PM
Last Post: joe_momma
  [Tkinter] Extrakt a Variable from a closed tkinter window hWp 5 3,877 Aug-23-2019, 09:01 PM
Last Post: woooee
  [WxPython] how to reopen a closed tab of wx.aui.Notebook? royer14 2 3,335 Feb-18-2019, 12:31 AM
Last Post: royer14
  [Tkinter] Completing Action when CheckBox is Checked Anysja 2 8,045 Aug-02-2018, 04:38 PM
Last Post: Anysja
  [Tkinter] Displaying an image that can't be closed nicochulo 4 3,684 Feb-15-2018, 10:27 AM
Last Post: nicochulo
  [PyQt] Whole application is closed by subwindows Alfalfa 1 7,433 Oct-03-2017, 03:22 PM
Last Post: Alfalfa
  doing something after been checked a checkbutton gray 1 4,327 Feb-18-2017, 05:11 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020