python - How can I optimize my code for my Spanish Translation Program? -


i student obtaining bachelor's in computer science, , writing python program in little spare time have me learn spanish. it's not extravagant program i'm going publish or anything, me have fun , mess around with. learned basic gui structure of python programming tkinter, , trying pointed in right direction optimize code make smaller , not basic. have 3500 lines of code far, won't upload whole code, here structure of pretty entire program

def _months( self ):     #framework month window     frame.__init__( self )     self.master.title( "months" )     self.grid()     labelfont = ( "times", 18, "bold" )     homefont = ( "times", 10, "bold" )      self._monthmenuimage = photoimage( file = 'mexicowater.gif' )     self._monthbackgroundlabel = label( self, image = self._monthmenuimage )     self._monthbackgroundlabel.place( x = 0, y = 0 )     self.grid_propagate(0)     self["height"] = 600     self["width"] = 800      #january button     self._januarybutton = button( self, text = "january",                                   command = self._switchjanuary )     self._januarybutton.config( font = labelfont, bg = self.water2 )     self._januarybutton.config( height = 0, width = 10 )     self._januarybutton.place( x = 65, y = 325 )      #february button     self._februarybutton = button( self, text = "february",                                    command = self._switchfebruary )     self._februarybutton.config( font = labelfont, bg = self.water2 )     self._februarybutton.config( height = 0, width = 10 )     self._februarybutton.place( x = 315, y = 325 )      #march button     self._marchbutton = button( self, text = "march",                                 command = self._switchmarch )     self._marchbutton.config( font = labelfont, bg = self.water2 )     self._marchbutton.config( height = 0, width = 10 )     self._marchbutton.place( x = 565, y = 325 ) 

the "command = self._switch...." leads different methods such as

 def _switchjanuary( self ):     if self._januarybutton["text"] == "january":         self._januarybutton.config( bg = self.water1 )         self._januarybutton["text"] = "enero"     else:         self._januarybutton["text"] = "january"         self._januarybutton.config( bg = self.water2 )  def _switchfebruary( self ):     if self._februarybutton["text"] == "february":         self._februarybutton.config( bg = self.water1 )         self._februarybutton["text"] = "febrero"     else:         self._februarybutton["text"] = "february"         self._februarybutton.config( bg = self.water2 )  def _switchmarch( self ):     if self._marchbutton["text"] == "march":         self._marchbutton.config( bg = self.water1 )         self._marchbutton["text"] = "marzo"     else:         self._marchbutton["text"] = "march"         self._marchbutton.config( bg = self.water2 ) 

"self.water1" , "self.water2" cool blue colors declared class variables earlier on.

this basic structure of entire code, months, days, numbers , such. i'm trying find way make code smaller, because want add lot of different features program without being million lines. i've been told use dictionary, in access key values translate words instead of having have each button lead different method, lost. appreciated. thank time!

the best way archive smarter (generic) code right level of abstraction. if closely see pattern in every of methods. in every method have 2 different strings. have mentioned perfect dictionary, english word remark key , spanish word value. have define dictionary needed word. can create buttons, reference generic method. generic method has button paramter. check if button-text matches keys, if not check if button-text matches values. hope idea now. here small working example:

from tkinter import tk, frame, button  def translate(button):     if button["text"] in monthdict.keys():         button.config(bg="red", text=monthdict[button["text"]])     else:         key, val in monthdict.iteritems():            if val in button["text"]:                button.config(bg="blue", text=key)  root = tk() mainframe = frame(root) mainframe.pack() monthdict = {"january": "enero", "february": "febrero", "march": "marzo"}  janbutton = button(mainframe, text="january", bg="blue", command= lambda: translate(janbutton)) janbutton.pack() febbutton = button(mainframe, text="february", bg="blue", command= lambda: translate(febbutton)) febbutton.pack() marchbutton = button(mainframe, text="march", bg="blue", command= lambda: translate(marchbutton)) marchbutton.pack() root.mainloop() 

even here can optimize. maybe smarter way key given value in translate method. or smarter way add buttons. example can use foreach create button. problem have find way pass button value function.

edit:

it kinda bugged me foreach didn't worked creating buttons right. solution use binding each button, event parameter access button again:

from tkinter import tk, frame, button  def translate(event):     if event.widget["text"] in monthdict.keys():         event.widget.config(bg="red", text=monthdict[event.widget["text"]])     else:         key, val in monthdict.iteritems():            if val in event.widget["text"]:                event.widget.config(bg="blue", text=key)  root = tk() mainframe = frame(root) mainframe.pack() monthdict = {"january": "enero", "february": "febrero", "march": "marzo"} buttondict = {}  key in monthdict.keys():     buttondict[key] = button(mainframe, text=key, bg="blue")     buttondict[key].bind("<button-1>", translate)     buttondict[key].pack()  root.mainloop() 

with buttondict still has access created buttons. if count right optimzed 120 lines of code 13 lines.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -