0

I'm having three buttons on a form, what I expect for example is when I click the 5% button it prints 5%. But for some reason for all buttons it print's 60%.

And I really don't understand why?

My code is below.

class ThirdWindow(QWidget, Ui_Form3):
    def __init__(self):
        super(ThirdWindow, self).__init__()
        self.dbu = DatabaseHandling.DatabaseUtility()
        self.msl = None

        # Show UI on screen + resize window
        self.setupUi(self)
        self.picIndicator.setPixmap(QPixmap("M:\QtProjects\\Resources\\138691.png"))
        self.setFixedSize(350, 480)

        #  Define what should happen on button click
        self.btnQuit.clicked.connect(lambda: self.close())
        self.btnSixtyPercent.clicked.connect(lambda: self.check_clicked())
        self.btnTenPercent.clicked.connect(lambda: self.check_clicked())
        self.btnFivePercent.clicked.connect(lambda: self.check_clicked())


    #  TODO: Create a window that ask's for the spot it's specific color
    '''def indication(self):
        d = DialogOne()
        d.exec_()'''

    def check_clicked(self):
        if self.btnSixtyPercent.text() == "60":
            print("60%")
        elif self.btnFivePercent.text() == "5":
            print("5%")
        elif self.btnTenPercent.text() == "10":
            print("10%")

Form

1 Answer 1

1

When you connect the signals to the slot, it executes when the signal is generated, if you want to work with the object that emits the signal you can use the sender().It is not necessary to use lambda functions.

    self.btnQuit.clicked.connect(self.close)
    self.btnSixtyPercent.clicked.connect(self.check_clicked)
    self.btnTenPercent.clicked.connect(self.check_clicked)
    self.btnFivePercent.clicked.connect(self.check_clicked)


def check_clicked(self):
        print("{}%".format(self.sender().text()

Complete code:

class ThirdWindow(QWidget, Ui_Form3):
    def __init__(self):
        super(ThirdWindow, self).__init__()
        self.dbu = DatabaseHandling.DatabaseUtility()
        self.msl = None

        # Show UI on screen + resize window
        self.setupUi(self)
        self.picIndicator.setPixmap(QPixmap("M:\QtProjects\\Resources\\138691.png"))
        self.setFixedSize(350, 480)

        #  Define what should happen on button click
        self.btnQuit.clicked.connect(self.close)
        self.btnSixtyPercent.clicked.connect(self.check_clicked)
        self.btnTenPercent.clicked.connect(self.check_clicked)
        self.btnFivePercent.clicked.connect(self.check_clicked)


    #  TODO: Create a window that ask's for the spot it's specific color
    '''def indication(self):
        d = DialogOne()
        d.exec_()'''

    def check_clicked(self):
        print("{}%".format(self.sender().text()))
Sign up to request clarification or add additional context in comments.

10 Comments

is the following possible?: If I remove the button text, and want to check which button is pressed using isChecked can you show me a quick example of that?
Of course it is possible since in sender() you will have the object, in that case the QCheckBox, and you can use its functions. On the other hand I think that my answer answered your doubt, if so I did it please mark it as correct.
For this case we must use the signal stateChanged, that can give us information of the button.
Can you add an example using my code? I would appreciate that a lot! :)
btnSixtyPercent, btnTenPercent, btnFivePercent are QCheckBoxs?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.