Page 1 of 1
Getting out of python
Posted: Mon May 22, 2017 4:20 pm
by bserce123
I'm trying to make a python script and using tkinter and a solid-state relay controlled thru the GPIO. I can make the SSR go on or off both from using Idle and from terminal but when I put a 'QUIT' button on the same Tk frame as the 'ON' and 'OFF' buttons that work and try to use the 'command= 'stoppit' ' on the button, in order to stop the app and clear the display, I can't find the code to make it work. I use:-
Def stoppit():
GPIO.output(24,0)
GPIO.cleanup()
killall
Nothing happens when I click on 'QUIT'. I have tried 'from os import kill' and ' from sys import exit' at the top and instead of killall use exit(0), kill, pkill and any combinations I can find instead after searching the net for a couple of weeks, I'm giving up and and asking here if someone will tell me exactly what code to use?
As this will be the only working app, on all day, on this pi, I am hoping later to write a shell script to reload the py script daily if the 'QUIT' thing ends up needing almost anything short of a reboot and my intervention, as I travel away a lot and this is to control a pump for irrigation. tvm for any help here.
Re: Getting out of python
Posted: Mon May 22, 2017 7:33 pm
by alphanumeric
I use
from python to stop my running python script.
Re: Getting out of python
Posted: Mon May 22, 2017 8:53 pm
by scotty101
Share your code. There is a specific tkinter way to call a function when the GUI is closed.
(I'd post it but I'm on my phone)
Re: Getting out of python
Posted: Mon May 22, 2017 9:17 pm
by bserce123
alphanumeric wrote:I use
from python to stop my running python script.
Thanks for the idea, but clicking on the button in the script is what I need to close things down and if I substitue 'raise SystemExit' instead of 'killall', nothing happens.
Re: Getting out of python
Posted: Mon May 22, 2017 9:31 pm
by bserce123
scotty101 wrote:Share your code. There is a specific tkinter way to call a function when the GUI is closed.
(I'd post it but I'm on my phone)
Sounds good. The button code in the Tk frame is :-
onb3=Button(rstrt,text='Override: Stop pump and Quit application', command=stoppit)
Looking out for your solution

Re: Getting out of python
Posted: Tue May 23, 2017 8:37 am
by scotty101
The example of doing this from tkinter reference material is
Code: Select all
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
The `root.protocol` line registers a function as a callback when the window is closed using the 'X' button or via the window manager. (You can also call this function when a button is clicked like you have done with your stoppit function)
Using 'root.destroy()' kills the root application and exits the tkinter mainloop, there for exiting the program.
Any tidying up that you want to do should be placed in the 'on_closing' function. For example you might do GPIO.cleanup() here.
(The messagebox is of course optional)
If this doesn't work for you, then you'll need to share your entire code file rather than just a single line.
Re: Getting out of python
Posted: Tue May 23, 2017 9:42 am
by alphanumeric
bserce123 wrote:alphanumeric wrote:I use
from python to stop my running python script.
Thanks for the idea, but clicking on the button in the script is what I need to close things down and if I substitue 'raise SystemExit' instead of 'killall', nothing happens.
I use the button (joy stick) on my sense hat to shutdown my Pi. It runs headless
Code: Select all
def pushed_middle(event):
global x
if event.action == ACTION_PRESSED:
x = 0
sense.stick.direction_middle = pushed_middle
if x == 0:
os.system("sudo shutdown now -P")
elif x == 1:
raise SystemExit
When I'm testing new code I change x=0 to x=1 so I can just stop my running program and not shut down.
I'm no python expert, and have no idea what tkinter is, so all I can tell you is what I posted above works OK for me in my python file. I used it a lot when I was first coding up my file. Stop, make edits, run, stop make edits run, etc Now that you posted your code, somebody should be able to help you sort it out.
Re: Getting out of python
Posted: Tue May 23, 2017 1:43 pm
by bserce123
scotty101 wrote:The example of doing this from tkinter reference material is
Code: Select all
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
The `root.protocol` line registers a function as a callback when the window is closed using the 'X' button or via the window manager. (You can also call this function when a button is clicked like you have done with your stoppit function)
Using 'root.destroy()' kills the root application and exits the tkinter mainloop, there for exiting the program.
Any tidying up that you want to do should be placed in the 'on_closing' function. For example you might do GPIO.cleanup() here.
(The messagebox is of course optional)
If this doesn't work for you, then you'll need to share your entire code file rather than just a single line.
I really am doing my best to learn Raspbian and python and solutions like that are super useful, I'll be off to digest and apply that when I get off the roof today, muchas gracias.
Re: Getting out of python
Posted: Tue May 23, 2017 1:47 pm
by bserce123
alphanumeric wrote:bserce123 wrote:alphanumeric wrote:I use
from python to stop my running python script.
Thanks for the idea, but clicking on the button in the script is what I need to close things down and if I substitue 'raise SystemExit' instead of 'killall', nothing happens.
I use the button (joy stick) on my sense hat to shutdown my Pi. It runs headless
Code: Select all
def pushed_middle(event):
global x
if event.action == ACTION_PRESSED:
x = 0
sense.stick.direction_middle = pushed_middle
if x == 0:
os.system("sudo shutdown now -P")
elif x == 1:
raise SystemExit
When I'm testing new code I change x=0 to x=1 so I can just stop my running program and not shut down.
I'm no python expert, and have no idea what tkinter is, so all I can tell you is what I posted above works OK for me in my python file. I used it a lot when I was first coding up my file. Stop, make edits, run, stop make edits run, etc Now that you posted your code, somebody should be able to help you sort it out.
Another valuable lesson, thank you, I'll read, mark, learn and inwardly digest.