I'm working on programming some application and I would like to create while loop when button is clicked and if it's clicked again to stop it. This is the code for button:
self.btnThisOne = gtk.Button("This one")
self.btnThisOne.connect("clicked", self.startLoop)
The code for startLoop def would be:
def startLoop(self):
while self.btnThisOne?(is_clicked)?:
#do something
How to do that?
Unfortunately, you cannot just have an unconstrained while loop running in the main thread of your application. That would block the main gtk event loop and you won't be able to process any more events. What you probably want to do is spawn a thread.
Have you considered using a
ToggleButton
instead ofGtkButton
? The closest thing to anis_clicked
method isis_active
and you'll find that in toggle buttons.Here's an example of starting and controlling a thread depending on the state of a toggle button (replace
triggered
withclicked
andToggleButton
withButton
if you want a regular button):This PyGTK FAQ answer might prove helpful. Cheers.