asdfj
Posts: 1
Joined: Sat Apr 06, 2013 1:46 am

Shutting Down in TKinter

Sat Apr 06, 2013 1:54 am

I've just read through all the other posts on this forum about shutting the pi down through something that looks like this:

import os
os.system("shutdown now")

Unfortunately, neither this nor any other variation of this code will work for my program. I am trying to add a menu item that allows the user to turn off the pi. Here's what I have:

filemenu.add_command(label='Shutdown', command = os.system("shutdown now"))

Early attempts (which looked similar but I didn't think to save them as they didn't work either) would shut the pi down immediately after I ran the module. Now, it doesn't do anything at all. Any help is appreciated!

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: Shutting Down in TKinter

Sat Apr 06, 2013 6:26 am

call(['sudo', 'shutdown', '-h', '-t 5','now'])

works for me when using Tkinter although I don't think anything special is needed for Tkinter.

Google man shutdown to see what the fields mean.

Also I don't thinkcommands in Tkinter menus can have parameters unless they are lambda ones.
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Shutting Down in TKinter

Sat Apr 06, 2013 4:04 pm

I would start by creating a function and tying it to the Tkinter command - something like:

Code: Select all


def my_handler_func():
    print "my_handler_func()"


...
    filemenu.add_command(label='Shutdown', command = my_handler_func) # or maybe: self.my_handler_func

When you get that print to come up, you can be sure that the binding to menu is ok - then replace with the os.system("sudo shutdown -h now").
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Python”