Python Forum
[closed] "checked" variable (attribute?) origin?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[closed] "checked" variable (attribute?) origin?
#1
Hi

I've found this small snippet, and in the the_button_was_toggled method, i do not understand from where the checked variable (attribute?) come from? Is it an inheritance?

Thanks

import sys

from PySide6.QtWidgets import (QApplication,
                               QMainWindow,
                               QPushButton,
                              )


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("My App")

        button = QPushButton("Press Me!")
        button.setCheckable(True)
        button.clicked.connect(self.the_button_was_clicked)
        button.clicked.connect(self.the_button_was_toggled)

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

    def the_button_was_clicked(self):
        print("Clicked!")

    def the_button_was_toggled(self, checked):
        print("Checked?", checked)


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec()
Reply
#2
Are you wondering wy button.clicked passes 1 argument (self) to "the_button_was_clicked" and two arguments to "the_button_was_toggled"?

I think your grabbed that snippet from here.
https://www.pythonguis.com/tutorials/pys...ts-events/

But this has a better description of signals and slots and how they relate to your particular example.
https://doc.qt.io/qtforpython-6.5/tutori...slots.html

The cliff notes version is the QPushButton.clicked signal has information about the toggled state of the button, even if the button cannot be toggled. When the clicked signal called "the_button_was_clicked()" the toggle state was not passed. When clicked signal called "the_button_was_toggled()", it passes the toggle state as a function argument. It knew to do this by inspecting the signature of the callback function.
Reply
#3
I can understand what the second argument checked in the slot is doing, but from where it comes from? no return, nor variable, nor attribute, etc. I had a quick look at the QmainWindows and QPushButton, but I haven't se nothing.

Thanks for your feedback
Reply
#4
Ok I think I finally figured out: if I want something to be printed (or checked) when the signal is sent, one need to define a variable that "sets" the boolean.

import sys
from PySide6.QtWidgets import QApplication, QPushButton


def Anyfunction(AnyArg_JustSomething):
    print(f"AnyArg_JustSomething  = {AnyArg_JustSomething}")


if __name__ == '__main__':
    app = QApplication(sys.argv)

    button = QPushButton("press me to send a signal")
    
    button.setCheckable(True)           # must be set to True to allow signal(s)
    button.toggled.connect(Anyfunction)    # signal

    button.show()
    sys.exit(app.exec())
Output:
AnyArg_JustSomething = True AnyArg_JustSomething = False AnyArg_JustSomething = True AnyArg_JustSomething = False AnyArg_JustSomething = True AnyArg_JustSomething = False ...
Gribouillis likes this post
Reply
#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


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