如何为我的西班牙语翻译计划优化我的代码?

时间:2016-04-25 03:04:00

标签: python optimization

我目前是一名获得计算机科学学士学位的学生,我正在闲暇时间写一篇蟒蛇计划,以帮助我学习西班牙语。它绝对不是我要发布的任何奢侈程序或任何东西,只是为了让我玩得开心和混乱。我刚刚学习了使用Tkinter进行python编程的基本GUI结构,我只是想指出正确的方向来优化我的代码,使其更小,看起来不那么基本。到目前为止我有3500行代码,所以我不会上传整个代码,但这是我整个程序的结构

def _months( self ):
    #Framework for the 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 )

"command = self._switch...."会导致不同的方法,例如

 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"只是很酷的蓝色颜色,我之前宣称为类变量。

这是我整个代码的基本结构,包括月,日,数字等。我只是想找到一种方法来缩小代码,因为我想为程序添加许多不同的功能,而不是百万行。我被告知要使用字典,我可以在其中访问键值来翻译单词而不必让每个Button导致不同的方法,但我迷路了。任何帮助将不胜感激。谢谢你的时间!

1 个答案:

答案 0 :(得分:2)

归档更智能(通用)代码的最佳方法是正确的抽象级别。如果你仔细观察,你会在每种方法中看到一种模式。在每种方法中,您有两个不同的字符串。就像你已经提到过的那样,它非常适合用于字典,其中英语单词注释键,西班牙语单词是值。你必须定义一个包含所有需要的单词的字典。您可以参考通用方法创建按钮。这种通用方法有一个Button作为参数。现在检查按钮文本是否与键匹配,如果不匹配,则检查按钮文本是否与这些值匹配。我希望你现在明白这个想法。 这是一个小工作示例:

from Tkinter import Tk, Frame, Button

def translate(button):
    if button["text"] in monthdict.keys():
        button.config(bg="Red", text=monthdict[button["text"]])
    else:
        for 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()

即使在这里你也可以优化。也许更聪明的方法是从translate方法中的给定值获取密钥。或者更智能的添加按钮的方法。例如,您可以使用foreach来创建按钮。他们唯一的问题是你必须找到一种方法将按钮作为值传递给函数。

修改

有点告诉我,foreach并没有正确地创建按钮。所以解决方案是对每个按钮使用绑定,使用event参数再次访问Button

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:
        for 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 = {}

for key in monthdict.keys():
    buttondict[key] = Button(mainframe, text=key, bg="Blue")
    buttondict[key].bind("<Button-1>", translate)
    buttondict[key].pack()

root.mainloop()

使用buttondict,您仍然可以访问创建的按钮。如果我把它算得恰到好处,将120行代码优化为13行。