Message: 4
Date: Sat, 19 Nov 2011 23:18:01 +0000
From: Alan Gauld <[email protected]>
To: [email protected]
Subject: Re: [Tutor] Can I shorten this code?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed


Most peoples code could be shortened. And most of it would be the worse
for it. There are a few occasions when shortening makes sense,
especially if its all repeated lines or just plain superflous code.
But, in general, aim to write readable code, not just short code.





--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/




**************************************



Thanks for your detailed answer, it is really appreciated :)
I understand now that it is really useful to have describing
names for a function so others can read/understand the code.

I have done my best to make the code more readable now. Here is the result.


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












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



However, I did not understand this part of your suggestions:

def change_global2_1():
global value1
value1=("green")

And this is completely redundant since it does exactly the same as the
other function, all you need is a parameter when you call it, like so:

def setValue1(val):
     global value1
     value1 = val

change_global2_1()

And this becomes

setValue1("green")

But ultimately you could just have set it directly in your outer
function, it's hardly worth the cost of defining a function, unless you
intend to add more to it later.

def change_value_hellobttn2(self):

All of the above comments apply here too.




Generally you build a data table with all the config parameters that
will varty then you build a loop to read the values from the data table.
Store the created buttons in a list or dictionary.

I must admit that I have never heard of a "data table" before. Is this easy to do, for
a beginner like me?


Thanks for your answers!



_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to