[Tutor] How to shorten this code using classes?

Mic o0MB0o at hotmail.se
Wed Nov 23 16:35:13 CET 2011


Hi.
I have written a simple GUI program. It works just like it is intended to 
do, but there
is a problem, I feel like I could make it shorter. I thought of using 
classes instead of
doing it the way I have done.

Here is my code:

from tkinter import*

button1_color="green"
button1_value=False

button2_color="green"
button2_value=False


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

    def create_widgets(self):

        #Creates hello button1
        self.hello_bttn1=Button(self,bg=button1_color, text="Hi_1",
command=self.button1_clicked)
        self.hello_bttn1.grid()

        #Creates hello button2
        self.hello_bttn2=Button(self,bg=button2_color, text="Hi_1",
command=self.button2_clicked)
        self.hello_bttn2.grid()



    def button1_clicked(self):
        """ This method runs if button one is clicked"""

        def change_button1_value():
            global button1_value
            button1_value=not button1_value

        change_button1_value()

        if button1_value:

            self.hello_bttn1.configure(bg="red", text="Hi_2")

            def change_button1_color_red():
                global button1_color
                button1_color=("red")
            change_button1_color_red()




        else:
            self.hello_bttn1.configure(bg="green", text="Hi_1")




            def change_button1_color_green():
                global button1_color
                button1_color=("green")
            change_button1_color_green()

            #-------------------------------------------------

    def button2_clicked(self):
        """This method runs if button two is clicked"""

        def change_button2_value():
            global button2_value
            button2_value=not button2_value

        change_button2_value()

        if button2_value:

            self.hello_bttn2.configure(bg="red", text="Hi_2")



            def change_button2_color_red():
                global button2_color
                button2_color=("red")
            change_button2_color_red()




        else:
            self.hello_bttn2.configure(text="Hi_1", bg="green")




            def change_button2_color_green():
                global button2_color
                button2_color=("green")
            change_button2_color_green()




As you can button1_clicked and button2_clicked are very similiar. Imagine
that I would use more buttons than just the two that I am using now.

It would be a lot of functions then, one for each button. How do I 
accomplish
the same thing as my code does now, but using one function for all buttons
instead of one function for each button? Is it possible?

I hope these questions aren't stupid.

Thank you for your time!


Mic














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



More information about the Tutor mailing list