User avatar
Eyh
Posts: 11
Joined: Mon Jan 25, 2021 5:44 pm

how do I implement the stop button to stop the program

Sun May 09, 2021 10:50 am

Code: Select all

import os
from guizero import App, Combo, Text, CheckBox, ButtonGroup, info, PushButton
def start_lxtask():
    os.system('lxtask')
def kill_lxtask():
    os.system('pkill -f lxtask')
app = App(title="How?")
start = PushButton(app, command=start_lxtask, text="start")
stop = PushButton(app, command=kill_lxtask, text="stop")
app.display()
When I press the start button i am not able to press the stop button to stop the program.
Help much appreciated

MiscBits
Posts: 249
Joined: Wed Jan 27, 2021 12:48 pm

Re: how do I implement the stop button to stop the program

Sun May 09, 2021 3:47 pm

What happens if you try:

Code: Select all

import os
from guizero import App, Combo, Text, CheckBox, ButtonGroup, info, PushButton
def start_lxtask():
    print('In start task button handler')
def kill_lxtask():
    print('In kill task button handler')
app = App(title="How?")
start = PushButton(app, command=start_lxtask, text="start")
stop = PushButton(app, command=kill_lxtask, text="stop")
app.display()
If you see the messages, the issue is in the pskill command.
I would also say you should disable the start button once the program has been started else you could have multiple lxtask jobs running and that may be an (or the) issue.


Note: Above is not tested as I have no Pi handy at the mo but based on this document.
Is a computer language with goto's totally Wirth-less?

User avatar
joelostinspace
Posts: 129
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: how do I implement the stop button to stop the program

Sun May 09, 2021 4:46 pm

Notice when you press the start button the button remains depressed? that's because the main loop of the gui is waiting for the program to terminate, you can't click on stop or even close the program. One way around this is with threads. https://docs.python.org/3/library/threading.html
here's an example:

Code: Select all

import os
from guizero import App, PushButton
from threading import Thread


def start_thread():
    start_= Thread(target=start_lxtask)
    start_.start()
    
def start_lxtask():
    os.system('lxtask')
    
def kill_lxtask():
    os.system('pkill -f lxtask')
    
app = App(title="This way!")

start = PushButton(app, command=start_thread, text="start")
stop = PushButton(app, command=kill_lxtask, text="stop")

app.display()

If a little knowledge is dangerous, where is the man who has so much as to be out of danger?


--- Thomas Huxley

User avatar
Eyh
Posts: 11
Joined: Mon Jan 25, 2021 5:44 pm

Re: how do I implement the stop button to stop the program

Sun May 09, 2021 4:52 pm

Thanks for the quick responses!
joelostinspace wrote: Notice when you press the start button the button remains depressed? that's because the main loop of the gui is waiting for the program to terminate, you can't click on stop or even close the program. One way around this is with threads. https://docs.python.org/3/library/threading.html
here's an example:

Code: Select all

import os
from guizero import App, PushButton
from threading import Thread


def start_thread():
    start_= Thread(target=start_lxtask)
    start_.start()
    
def start_lxtask():
    os.system('lxtask')
    
def kill_lxtask():
    os.system('pkill -f lxtask')
    
app = App(title="This way!")

start = PushButton(app, command=start_thread, text="start")
stop = PushButton(app, command=kill_lxtask, text="stop")

app.display()
This worked brilliantly! Just the solution i was looking for! I was wondering if there was a continuous loop running and if yes how could i stop it, this answers it completely.
Being a newbie i didn't know about threading.
Thanks for all your help!

Return to “Python”