Skip to main content
 首页 » 编程设计

python之如何优化我的西类牙语翻译程序代码

2024年10月24日7bhlsheji

我目前是一名获得计算机科学学士学位的学生,我正在利用很少的业余时间编写一个 python 程序,以帮助我学习西类牙语。这绝对不是我要发布的奢侈节目或任何东西,只是为了让我玩得开心和乱七八糟。我刚刚学习了使用 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 作为参数。现在检查按钮文本是否与键匹配,如果不匹配则检查按钮文本是否与这些值匹配。我希望你现在明白了。 这是一个小的工作示例:

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 无法正确创建按钮,这让我有点烦恼。所以解决方案是为每个按钮使用绑定(bind),使用 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 行。