Broken_Mind
Posts: 25
Joined: Fri Aug 03, 2012 8:32 am
Location: DE/BW/RT

Start python script on boot

Sat Apr 19, 2014 10:33 pm

Hello,

I'm trying currently in Python and I have a few questions to it.
- How can I start the python script at boot?
- How can I adjust my window as a full screen window?
- How can I place the buttons in a fix position?

Here is my code:

Code: Select all

HOST = "127.0.0.1"
PORT = 4223
UID = "jNw"

from Tkinter import *
from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_remote_switch import RemoteSwitch
import os
master = Tk()

master.title("GUI")

w = Canvas(master, width=300, height=200)
w.pack()

def shutdown():
    os.system("sudo shutdown -h now")

def reboot():
    os.system("sudo shutdown -r now")

def light_on():
    if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    rs = RemoteSwitch(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    # Switch socket with house code 17 and receiver code 1 on.
    # House code 17 is 10001 in binary (least-significant bit first)
    # and means that the DIP switches 1 and 5 are on and 2-4 are off.
    # Receiver code 1 is 10000 in binary (least-significant bit first)
    # and means that the DIP switch A is on and B-E are off.
    rs.switch_socket_b(1, 1, RemoteSwitch.SWITCH_TO_ON)

def light_off():
    if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    rs = RemoteSwitch(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    # Switch socket with house code 17 and receiver code 1 on.
    # House code 17 is 10001 in binary (least-significant bit first)
    # and means that the DIP switches 1 and 5 are on and 2-4 are off.
    # Receiver code 1 is 10000 in binary (least-significant bit first)
    # and means that the DIP switch A is on and B-E are off.
    rs.switch_socket_b(1, 1, RemoteSwitch.SWITCH_TO_OFF)

b=Button(master, text= "Shutdown", command=shutdown)
b.pack()

b=Button(master, text= "Reboot", command=reboot)
b.pack()

b=Button(master, text= "Licht an", command=light_on)
b.pack()

b=Button(master, text= "Licht aus", command=light_off)
b.pack()

mainloop()
friendly regards

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Start python script on boot

Sun Apr 20, 2014 6:33 am

To run your script at startup open a terminal window and type sudo nano /etc/rc.local
At the end of this file add python scriptname.py & (where scriptname is the name of your script) and save that.
For full screen in Tkinter look here http://stackoverflow.com/questions/7966 ... on-tkinter and for better widget placement use grid instead of pack.

Dave.
Apple say... Monkey do !!

gkreidl
Posts: 6326
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Start python script on boot

Sun Apr 20, 2014 6:56 am

full screen:
master.attributes('-fullscreen', True)
positioning elements:
use the grid() method and the sticky attribute; it's a bit "tricky". Here's a fine tutorial for TKinter:
http://infohost.nmt.edu/tcc/help/pubs/t ... index.html

For auto starting X, a windowmanager and your GUI application without going to the desktop you may use the method I've described in my Minimal Kiosk Browser manual. Just replace 'kweb' with your application.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

Broken_Mind
Posts: 25
Joined: Fri Aug 03, 2012 8:32 am
Location: DE/BW/RT

Re: Start python script on boot

Tue Apr 22, 2014 10:26 am

I tried it with Grid. But I did not work. Could you show me an example for a button?

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Start python script on boot

Tue Apr 22, 2014 10:55 am

To run your script at startup open a terminal window and type sudo nano /etc/rc.local
Wrong!
(See: http://en.wikipedia.org/wiki/John_McLaughlin_(host))

Search the forum for "Best way".
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Return to “Python”