ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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).
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.