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

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Python-tk question


    I have been trying to write a simple GUI that creates a button that will change colors from default gray to red when clicked...

    Code:
    #!/usr/bin/python
    
    from Tkinter import *
    
    master = Tk()
    
    def color_change(self):
    	self.button1.configure(bg = "red")
    
    frame1 = Frame(master, height = 300, width = 300)
    frame1.pack_propagate(0)
    frame1.pack()
    
    button1 = Button(frame1, text = "Click Me", command = color_change)
    button1.pack()
    
    mainloop()
    I'm getting an error that color_change() takes exactly one argument in my terminal when I try to run the script. I'm not sure what is causing the error.

    Also, what does "self" do? I can't find an explaination of using "self", I have searched several sites and pieced together some things I found to get this, but I'm not sure how "self" effects the script.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    438
    Rep Power
    67
    Originally Posted by secmlt
    I'm getting an error that color_change() takes exactly one argument in my terminal when I try to run the script. I'm not sure what is causing the error.
    Well of course the functions expects an argument if you write it to expect an argument:

    Code:
    def color_change(self):
        self.button1.configure(bg = "red")
    Change this to:

    Code:
    def color_change():
        button1.configure(bg = "red")
    ...and your code works as expected.

    “self” is the name ordinarily used to refer to a class instance inside a class definition. You haven’t defined any classes in your code.

    Comments on this post

    • Dietrich agrees : good explanation
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Thanks for the response, but I've got another question... I have the following code:

    Code:
    #!/usr/bin/python
    
    from Tkinter import *
    import subprocess
    
    master = Tk()
    path = "/path/to/perl_script.pl"
    
    def color_change():
    	perl_result = subprocess.call([path])
    
    	if (perl_result == 0):
    		button1.configure(bg = "red")
    	else:
    		button1.configure(bg = "green")
    
    frame1 = Frame(master, height = 300, width = 300)
    frame1.pack_propagate(0)
    frame1.pack()
    
    button1 = Button(frame1, text = "Click Me", command = color_change)
    button1.pack()
    
    mainloop()
    The program calls a perl script when the button is clicked that generates a random number (either 0 or 1) and the button that was clicked changes colors based on the result of the perl script. However, when I mouse over the button, the button always changes to gray. Is there anyway to make the button stay either green or red instead of turning gray when the mouse pointer is over the button?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    Try various settings of these puppies:

    activebackground
    activeforeground
    background
    bitmap
    disabledforeground
    foreground
    highlightbackground
    highlightcolor
    highlightthickness
    image

    Your code will use less resources if you have python generate the random number.

    random.choice((0,1))
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Try various settings of these puppies:

    activebackground
    activeforeground
    background
    bitmap
    disabledforeground
    foreground
    highlightbackground
    highlightcolor
    highlightthickness
    image

    Your code will use less resources if you have python generate the random number.

    random.choice((0,1))
    The "activebackground" combined with "background" gave me what I needed. Thanks for the help! Also, do you know of a website that explains the functions of python-tk?

    I cannot use python to generate a random number unfortunately, because this is a project for work and my co-workers are obsessed with perl scripts... I'm the only one that uses python. My GUI has to be able to interface with perl scripts that spit out random numbers (it actually won't be random numbers but ping results that make the buttons change color).
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Try:
    http://www.tkdocs.com/tutorial/index.html

    Note that tk is available for a number of computer languages including Perl.
    Last edited by Dietrich; December 29th, 2012 at 09:56 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo