Python Forum

Full Version: keeping track of pushbutton click
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,

Due to the slightly annoying thing about not being able to return a value from a method when a button is clicked, how do you keep track of button clicks. I have a simple message window, and the messages are downloaded into a list and I want to cycle through this list so usually, you could set up a simple while false loop and iterate through,  but I can't work out how you are supposed to do that with qt, if you have to call another method and can't pass variables back and forth

How is this done?
Not sure how it's done in Qt, but the button must create an event when clicked. That
event must to bound to a function that gets executed when the event is generated. In
tkinter, that is done by using a bind statement associated with the button, or through a
command attribute in the Buttons instance. I seem to recall an on_click attribute in qt that can be set in a similar manner.
Yes, it's the same in Qt, the only problem is you can't pass any data to or from the function being called. My work around is to use Global variables, but I don't see this as a good way to do this especially as this is the only reason to use a global variable in the whole of the application.
Are you coding from an editor, or using designer?
If from an editor, you should be able to add an argument by using a lambda statement.
I'll find an example (not in Qt, but should work the same)
and post it jast as soon as I do

Example:
b1 = tk.Button(self.f1, text="Page 1", command=lambda: self.show_page(self.page1), width=8, height=2)