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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-21-2003, 06:09 PM
|
#1
|
|
Senior Member
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142
Rep:
|
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?
|
|
|
|
07-22-2003, 02:50 PM
|
#2
|
|
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794
Rep: 
|
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 02:51 PM.
|
|
|
|
07-24-2003, 01:21 PM
|
#3
|
|
Member
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569
Rep:
|
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 01:23 PM.
|
|
|
|
07-24-2003, 08:31 PM
|
#4
|
|
Senior Member
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142
Original Poster
Rep:
|
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)
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:00 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|