[英]How can I optimize my code for my Spanish Translation Program?
我目前是一名获得计算机科学学士学位的学生,我正在用很少的空闲时间编写一个 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 指向不同的方法,但我迷路了。 任何帮助将不胜感激。 感谢您的时间!
存档更智能(通用)代码的最佳方法是正确的抽象级别。 如果你仔细观察,你会发现每一种方法都有一个模式。 在每种方法中,您都有两个不同的字符串。 就像您已经提到的那样,它非常适合字典,其中英语单词 remark 键,西班牙语单词是 value。 你必须用所有需要的词来定义一个字典。 参考您的通用方法,您可以创建您的按钮。 这个通用方法有一个 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 行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.