[英]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.