[Tutor] How to shorten this code using classes?

James Reynolds eire1130 at gmail.com
Wed Nov 23 18:09:14 CET 2011


On Wed, Nov 23, 2011 at 10:35 AM, Mic <o0MB0o at hotmail.se> wrote:

> 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()
>
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>








Button 1 and Button 2 already are classes, so unless you need to subclass,
you are simply being redundant.

A couple things first: don't use globals all over the place unless you want
to have a serious headache.

You have a wicked about of redundancy in your code, you don't need an
object oriented approach to condense, but it can be helpful at times.

However, as far as your question. For the two functions, you could subclass
Button and add them to your new Button object:

(untested)


class MyButton(Button):
    button_color="green"
    button_value=False

    def __init__(self, *args, **kwargs):
        super(Button, self).__init__(*args, **kwargs)

    def change_value_and_color(self):
        self.button_value = not self.button_value
        if self.button_value:
            self.button_color="red"
            self.configure(bg=self.button_color, text="Hi_2")
        else:
            self.button_color="green"
            self.hello_bttn1.configure(bg=self.button_value, text="Hi_1")

    def button_clicked(self):
        """ This method runs if button one is clicked"""
        self.change_value_and_color()


I left in the button_clicked method, but as you can see it is utterly
pointless to have it. Just rename your first function and call it directly.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111123/9eb2d550/attachment-0001.html>


More information about the Tutor mailing list