[Tutor] Shortening the code of a finsihed program.

Mic o0MB0o at hotmail.se
Fri Nov 25 22:14:46 CET 2011


>Its usually better to paste long programs into a pastebin web site and
>give us a link.
>This saves cluttering up people mail with long messages, and also the
>pastebin rendering will usually be easier to read with syntax coloring etc.

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?





>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.
>Its nearly always better to declare all your classes at the top level of
>the program.


Why is it restricting?


>Because you post lost all the formatting, I'm not sure where this code
>is supposed to sit... This is again where a pastebin would help.

Oh, now I understand why I shouldn't post like this. My apologize, won't 
happen again.


>And this is repeating all the bad habits from your original posts. If
>you adopt the ChairButton class approach all of this becomes much
>clearer and simpler.

I got a really good tip earlier on in the thread how I could make my program
dramatically shorter. I have tried to implement a couple of things I need
into that piece of code, but I cannot get it to work.

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")



class Window(tk.Frame):
    def __init__(self, master):
        super (Window, self).__init__(master)
        self.grid()
        self.create_widgets()


    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()


root = tk.Tk()
root.title("Test")
root.geometry("200x200")
app = Window(root)
root.mainloop()


Questions:

--I want the first button to be created to have the text "1". The second 
should have the text "2".
  The third the text "3" and so on.

--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.

  When the second button is pressed I want a file to be created with the 
name Germany_France_2.
   The text in the file should be Germany_France_2.
   If/When the button is pressed again it should delete the file.

  When the third button is pressed I want a file to be created with the name 
Germany_France_3.
   The text in the file should be Germany_France_3.
   If/When the button is pressed again it should delete the file.



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.

However, to my dismay, I have tried this entire evening without success. It 
feels like I am so close
to shorten my program from 2k+ lines of code to maybe 250 at most, but still 
so far away.










>You should only ever have one Tk() object in a Tkinter program.

Why is that?


>And again you should only have one mainloop running, otherwise things
>would get very confusing with events winding up in the wrong windows
>event queue etc.

Alright, my teacher never mentioned that when he examined my program.
Thanks for your input! Your help is as always appreciated and my apologize
for making confusing posts!

Mic 



More information about the Tutor mailing list