[Tutor] Shortening the code of a finsihed program.
Alan Gauld
alan.gauld at btinternet.com
Fri Nov 25 22:50:10 CET 2011
On 25/11/11 21:14, Mic wrote:
> Alright. Sorry if I should know this, but what is a pastebin web site
> and how do I paste my program into a pastebin web site?
A web site that you can paste stuff and then provide a link(url) that
others can use to view it.
You can usually specify the code style and it will apply syntax coloring
for you.
Try a google search for free pastebin...
As an example I've pasted your code from this message at:
http://pastebin.com/H3VzaapV
Take a look and you will see what I mean.
>> While its perfectly legal Python to create a class inside a method its
>> very unusual in practice and very restricting in the use of the class.
>
> Why is it restricting?
Because the class is only available inside the function. You cannot
create objects of that class anywhere outside the class.
> So I figured I could post it here (short code) and ask you a couple of
> questions regarding the code:
>
>
> import tkinter as tk
> from functools import partial
>
> def button_clicked(button):
> if button["bg"] == "green":
> button.configure(bg="red", text="01")
> else:
> button.configure(bg="green", text="01")
Note you are setting text to '01' in every case. Thats probably not what
you want?
> def create_widgets(self):
> list_chair=[(0, 0), (0, 1), (0, 3), (0, 4), (1,0)]
> for row, column in list_chair:
> button = tk.Button(self)
> command = partial(button_clicked, button)
> button["command"] = command
> button.grid(row=row, column=column)
> command()
As stated above this will result in every chair being green
and having the text '01' It would be better to put the initial colour
and text in your data list and configure each button directly:
def create_widgets(self):
list_chair=[(0, 0, '01'), (0, 1, '02'),
(0, 3, '03'), (0, 4, '04'),
(1, 0, '05')]
for row, column, name in list_chair:
command = partial(button_clicked, button)
button = tk.Button(self, color='green',
command=command, text=name)
button.grid(row=row, column=column)
Incidentally, This is what I mentioned early on in the
discussion about using a data table to create your widgets.
> root = tk.Tk()
> root.title("Test")
> root.geometry("200x200")
> app = Window(root)
> root.mainloop()
> --When the first button is pressed I want a file to be created with the
> name Germany_France_1.
> The text in the file should be Germany_France_1.
> If/When the button is pressed again it should delete the file.
Lets get the initial UI set up correctly first.
Once we have done that we can worry about adding the functionality.
This is one of the good habits in programming. Do it step by step. Get
one thing working first before trying to add more features. Its a lot
easier to debug code when you know what you changed to stop it working.
> Do you have any idea on how I can accomplish this? I reached the
> conclusion that it should be easy to do so since it was so easy
> to create so many buttons in so little amount of code.
Yes, but lets get the UI all done first, then we can add the button
features.
>> You should only ever have one Tk() object in a Tkinter program.
>
> Why is that?
Because thats how Tkinter expects to work! It builds a tree of all the
windows and widgets in your program. If you have two trees in the same
program it can get very confused about which widget is doing what to
which other widgets, especially if they wind up in different trees!.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list