Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 18th, 2012, 09:23 PM
secmlt secmlt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 secmlt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 20 sec
Reputation 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.

Reply With Quote
  #2  
Old December 19th, 2012, 03:23 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 17 m 59 sec
Reputation Power: 65
Quote:
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #3  
Old December 28th, 2012, 03:04 PM
secmlt secmlt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 secmlt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 20 sec
Reputation 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?

Reply With Quote
  #4  
Old December 28th, 2012, 03:58 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,393 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 37 m 10 sec
Reputation Power: 383
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!

Reply With Quote
  #5  
Old December 29th, 2012, 09:44 PM
secmlt secmlt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 secmlt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 20 sec
Reputation Power: 0
Quote:
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).

Reply With Quote
  #6  
Old December 29th, 2012, 09:54 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 484 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 23 h 21 m 50 sec
Reputation Power: 63
Try:
http://www.tkdocs.com/tutorial/index.html

Note that tk is available for a number of computer languages including Perl.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Last edited by Dietrich : December 29th, 2012 at 09:56 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python-tk question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap