LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-21-2003, 07:09 PM   #1
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
Python EXEC Statement


Anybody know how to use the Python exec statement? I'm working on a GUI, and need to change the colors/states of a number of buttons when one is pushed. If I print cmd instead of exec, the lines come out right.
Code:
  buttons = [('nw', 'black'),
             ('n',  'black'),
             ('ne', 'red'),
             ('w',  'black'),
             ('e',  'black'),
             ('sw', 'black'),
             ('s',  'black'),
             ('se', 'black')]
  for (dir, clr) in buttons:
    cmd = "wd_" + dir + "_button.config(color='" + clr +"')"
    exec cmd
When run, I get the error...
Code:
Traceback (most recent call last):
  File "/usr/lib/python2.2/lib-tk/Tkinter.py", line 1316, in __call__
    return apply(self.func, args)
  File "weather2.py", line 58, in wd_ne
    exec cmd
  File "<string>", line 1, in ?
  File "/usr/lib/python2.2/lib-tk/Tkinter.py", line 1109, in configure
    self.tk.call((self._w, 'configure')
TclError: unknown option "-color"
I'm looking for a better way then writing out...
Code:
wd_nw_button.config(color='black')
...eight times, and five similar lines to change states on air_pressure buttons, and five similar lines to change states for pressure_change buttons, for each of the eight wind_directions. What is that...? Nearly 160 lines?
 
Old 07-22-2003, 03:50 PM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Maybe try checking the Documentation, specifically code or cmd ?

A better way may be to change your algorithm. Maybe keep a tract of current_red_button and pushed_button, and only change their colours around.
Or alter the button pushed event responce to something like reset_all_buttons(), make_button_red(this).

That's some really fast bad pseudo code btw

Last edited by Proud; 07-22-2003 at 03:51 PM.
 
Old 07-24-2003, 02:21 PM   #3
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Yeah, your approach is bad (actually it sucks, but I don't want to say that as it sounds kinda negative ). Don't worry, I used to want to do that sort of thing as well.

The real solution you want is to store the buttons themselves in a dict.

Code:
wd_buttons = {'nw': Button('nw'), 'n': Button('n'), ... }
(note: I don't know how you create those buttons, but whatever you do to create them is represented by the Button() calls above.)

Then your code above simply becomes:
Code:
  buttons = [('nw', 'black'),
             ('n',  'black'),
             ('ne', 'red'),
             ('w',  'black'),
             ('e',  'black'),
             ('sw', 'black'),
             ('s',  'black'),
             ('se', 'black')]
  for (dir, clr) in buttons:
    wd_buttons[dir].config(color=clr)

Last edited by Strike; 07-24-2003 at 02:23 PM.
 
Old 07-24-2003, 09:31 PM   #4
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Original Poster
Rep: Reputation: 47
Turns out, it's even easier...
Code:
buttons = [(wd_nw_button, 'black'),
           (wd_n_button,  'red'),
           (wd_ne_button, 'black'),
           (wd_w_button,  'black'),
           (wd_e_button,  'black'),
           (wd_sw_button, 'black'),
           (wd_s_button,  'black'),
           (wd_se_button, 'black')]
for (dir, clr) in buttons:
  dir.config(fg=clr)     # or... dir["fg"] = clr
This is my first GUI program, so I knew it would be ugly. I finally found it in the Tkinter module doc.

Of course, I had already implemented a way like Proud had suggested... Each button push first calls a function that turns all the buttons black, then turns the one pushed red. Only need to write out the color='black' lines once.

I still might use the idea above though, so I can pass any button_name, attribute, and setting to a function. Such as ... button_config(ap_hi_button, state, NORMAL)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 08:02 PM
installing python library's (Python Numeric) Four Linux - Newbie 1 10-16-2005 03:31 PM
Python ignores global statement? voyciz Programming 3 04-08-2005 03:07 PM
install python 2.3 ,necssary to remove python 2.2 ngan_yine Linux - Newbie 7 12-28-2003 05:07 PM
Apache and mod_python not exec python scripts fIREfox Linux - Networking 1 02-11-2003 06:54 PM


All times are GMT -5. The time now is 12:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration