#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Help with some basic Python programming


    Hello,

    I need some help with some basic Python programming. I need to make a program that makes it when I press a key on my keyboard it changes the color of the screen dependent on the key pressed. For example if I pressed the key r then the screen would turn red. I am quite inexperienced and any help would be greatly appreciated.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    What part of the program are you willing to write?
    Do you want to understand the program?
    What do you mean by "the screen"?
    How is its color changed?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    What part of the program are you willing to write?
    Do you want to understand the program?
    What do you mean by "the screen"?
    How is its color changed?
    For my science fair project I am changing the color of light with a LCD panel for the purpose of modifying the growth of plants in a greenhouse. What I need is a program that allows me to press a key and have the screen change color. I am using a raspberry pi for this project hence using python. I am willing to write all of it, but I need some advice on how to start and how to structure the code most efficiently. That said having some of the code written for me as a starting point would also be greatly appreciated.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Code:
    # the easy way would be (have been) to use xsetroot
    # xsetroot -solid red
    
    import sys
    
    tkinter = None
    
    try:
        import tkinter
    except:
        try:
            import Tkinter as tkinter
        except:
            pass
    
    if tkinter == None:
        sys.stderr.write('cannot find tkinter')
        sys.exit(1)
    
    colors = dict(
        r = 'red',
        g = 'green',
        b = 'blue',
    
        m = 'magenta',
        y = 'yellow',
        c = 'cyan',
    
        w = 'white',
        o = 'black',  #off
        #ryb present
    )
    
    def change_color(event):
        w = event.widget
        c = event.char.lower()
        if c == 'q':
            w.destroy()
            w.quit()
        if c in colors:
            w.config(background=colors[c])
    
    root = tkinter.Tk()
    canvas = tkinter.Canvas(root, width=root.winfo_vrootwidth(), height=root.winfo_vrootheight())
    canvas.bind('<KeyPress>',change_color)
    canvas.pack()
    canvas.focus_set()
    root.mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo