#はじめに
HCB Advent Clendar 2021の14日目を担当するKimです。
今回は,TKinterでEntryとLabelの生成動作を関数化して,起動時の生成コード量を80%減らした時のことを書きます.
#作業環境
- Python 3.7.12
- Tkinter 3.6
##生成するUI一覧
- Label 7個
- Entry 8個
#Before
##生成コード
def tab3_main(self,tab3):
gestureLabel = tk.Label(tab3, text="脚部動作")
gestureLabel.grid(row=0, column=0)
inputKeyLabel_L = tk.Label(tab3, text="左足入力キー")
inputKeyLabel_L.grid(row=0, column=1)
inputKeyLabel_R = tk.Label(tab3, text="右足入力キー")
inputKeyLabel_R.grid(row=0, column=2)
ThighLiftLabel = tk.Label(tab3, text="太もも上げ")
ThighLiftLabel.grid(row=1, column=0)
ThiLiftStr = tk.StringVar()
HeelUpLabel = tk.Label(tab3, text="かかと上げ")
HeelUpLabel.grid(row=2, column=0)
HeelUpStr = tk.StringVar()
LeftKickLabel = tk.Label(tab3, text="左蹴り")
LeftKickLabel.grid(row=3, column=0)
LeftKickStr = tk.StringVar()
RightKickLabel = tk.Label(tab3, text="右蹴り")
RightKickLabel.grid(row=4, column=0)
RightKickStr = tk.StringVar()
ThighLiftEntry_L = tk.Entry(tab3, validate="key", textvariable=ThiLiftStr,justify="center",width=10)
ThighLiftEntry_L.bind("<FocusIn>", lambda e:self.focus(ThighLiftEntry_L,self.FootMotion_L_Stand["太もも上げ"]))
ThighLiftEntry_L.bind("<Any-KeyPress>", lambda e:self.insert(e,ThighLiftEntry_L,self.FootMotion_L_Stand["太もも上げ"]))
ThighLiftEntry_L.grid(row=1, column=1)
ThighLiftEntry_R = tk.Entry(tab3, validate="key", textvariable=ThiLiftStr,justify="center",width=10)
ThighLiftEntry_R.bind("<FocusIn>", lambda e:self.focus(ThighLiftEntry_R,self.FootMotion_L_Stand["太もも上げ"]))
ThighLiftEntry_R.bind("<Any-KeyPress>", lambda e:self.insert(e,ThighLiftEntry_R,self.FootMotion_R_Stand["太もも上げ"]))
ThighLiftEntry_R.grid(row=1, column=2)
HeelUpEntry_L = tk.Entry(tab3, validate="key", textvariable=HeelUpStr,justify="center",width=10)
HeelUpEntry_L.bind("<FocusIn>", lambda e:self.focus(HeelUpEntry_L,self.FootMotion_L_Stand["かかと上げ"]))
HeelUpEntry_L.bind("<Any-KeyPress>", lambda e:self.insert(e,HeelUpEntry_L,self.FootMotion_L_Stand["かかと上げ"]))
HeelUpEntry_L.grid(row=2, column=1)
HeelUpEntry_R = tk.Entry(tab3, validate="key", textvariable=HeelUpStr,justify="center",width=10)
HeelUpEntry_R.bind("<FocusIn>", lambda e:self.focus(HeelUpEntry_R,self.FootMotion_L_Stand["かかと上げ"]))
HeelUpEntry_R.bind("<Any-KeyPress>", lambda e:self.insert(e,HeelUpEntry_R,self.FootMotion_R_Stand["かかと上げ"]))
HeelUpEntry_R.grid(row=2, column=2)
LeftKickEntry_L = tk.Entry(tab3, validate="key", textvariable=LeftKickStr,justify="center",width=10)
LeftKickEntry_L.bind("<FocusIn>", lambda e:self.focus(LeftKickEntry_L,self.FootMotion_L_Stand["左キック"]))
LeftKickEntry_L.bind("<Any-KeyPress>", lambda e:self.insert(e,LeftKickEntry_L,self.FootMotion_L_Stand["左蹴り"]))
LeftKickEntry_L.grid(row=3, column=1)
LeftKickEntry_R = tk.Entry(tab3, validate="key", textvariable=LeftKickStr,justify="center",width=10)
LeftKickEntry_R.bind("<FocusIn>", lambda e:self.focus(LeftKickEntry_R,self.FootMotion_L_Stand["左キック"]))
LeftKickEntry_R.bind("<Any-KeyPress>", lambda e:self.insert(e,LeftKickEntry_R,self.FootMotion_R_Stand["左蹴り"]))
LeftKickEntry_R.grid(row=3, column=2)
RightKickEntry_L = tk.Entry(tab3, validate="key", textvariable=RightKickStr,justify="center",width=10)
RightKickEntry_L.bind("<FocusIn>", lambda e:self.focus(RightKickEntry_L,self.FootMotion_L_Stand["右キック"]))
RightKickEntry_L.bind("<Any-KeyPress>", lambda e:self.insert(e,RightKickEntry_L,self.FootMotion_L_Stand["右蹴り"]))
RightKickEntry_L.grid(row=4, column=1)
RightKickEntry_R = tk.Entry(tab3, validate="key", textvariable=RightKickStr,justify="center",width=10)
RightKickEntry_R.bind("<FocusIn>", lambda e:self.focus(RightKickEntry_R,self.FootMotion_L_Stand["右キック"]))
RightKickEntry_R.bind("<Any-KeyPress>", lambda e:self.insert(e,RightKickEntry_R,self.FootMotion_R_Stand["右蹴り"]))
RightKickEntry_R.grid(row=4, column=2)
行数:51行(余白除く)
#After
##生成用クラスの作成
class MotionLabelAndKeyEntry():
def setKeys(self,inputedList:list,inputKey:str):
inputedList.append(inputKey)
print(inputedList)
def deleteKeys(self,inputedList:list):
inputedList.pop()
def focus(self,selectEntry:tk.Entry,inputedList:list):
selectEntry.delete(0, tk.END)
inputedList.clear()
def insert(self,event,selectEntry:tk.Entry,keys=[]):
pressedKey = event.keysym
if (pressedKey == "Meta_L" or pressedKey == "Meta_R"):
selectEntry.insert(tk.END, u'⌘')
self.setKeys(keys,"command")
elif (pressedKey == "Alt_L"):
selectEntry.insert(tk.END, u'⌥')
self.setKeys(keys,"option")
elif (pressedKey == "Control_L"):
selectEntry.insert(tk.END, u'⌃')
self.setKeys(keys,"ctrl")
elif (pressedKey == "Shift_L" or pressedKey == "Shift_R"):
selectEntry.insert(tk.END, u'⇧')
self.setKeys(keys,"Shift")
elif pressedKey == "Down":
selectEntry.insert(tk.END, u'↓')
self.setKeys(keys,"Down")
elif pressedKey == "Left":
selectEntry.insert(tk.END, u'←')
self.setKeys(keys,"Left")
elif pressedKey == "Right":
selectEntry.insert(tk.END, u'→')
self.setKeys(keys,"Right")
elif pressedKey == "Up":
selectEntry.insert(tk.END, u'↑')
self.setKeys(keys,"Up")
elif pressedKey == "BackSpace":
self.deleteKeys(keys)
else:
self.setKeys(keys,pressedKey)
def createTopLabels(self,tab:tk.Frame):
gestureLabel = tk.Label(tab, text="脚部動作")
gestureLabel.grid(row=0, column=0)
inputKeyLabel_L = tk.Label(tab, text="左足入力キー")
inputKeyLabel_L.grid(row=0, column=1)
inputKeyLabel_R = tk.Label(tab, text="右足入力キー")
inputKeyLabel_R.grid(row=0, column=2)
def createLabelsAndEntrys(self,tab:tk.Frame,label_text:str,Label_row:int,Label_column:int,KeyList:list,Entry_row=int,Entry_column=int):
gestureLabel = tk.Label(tab, text=label_text)
gestureLabel.grid(row=Label_row, column=Label_column)
inputKey = tk.StringVar()
Entry = tk.Entry(tab, validate="key", textvariable=inputKey,justify="center",width=10)
Entry.bind("<FocusIn>", lambda e:self.focus(Entry,KeyList))
Entry.bind("<Any-KeyPress>", lambda e:self.insert(e,Entry,KeyList))
Entry.grid(row=Entry_row, column=Entry_column)
##生成コード
def tab3_main(self, tab3):
UI = MotionLabelAndKeyEntry()
UI.createTopLabels(tab3)
UI.createLabelsAndEntrys(tab3, "太もも上げ", 1, 0, self.FootMotion_L_Stand["太もも上げ"], 1, 1)
UI.createLabelsAndEntrys(tab3, "太もも上げ", 1, 0, self.FootMotion_R_Stand["太もも上げ"], 1, 2)
UI.createLabelsAndEntrys(tab3, "かかと上げ", 2, 0, self.FootMotion_R_Stand["かかと上げ"], 2, 1)
UI.createLabelsAndEntrys(tab3, "かかと上げ", 2, 0, self.FootMotion_R_Stand["かかと上げ"], 2, 2)
UI.createLabelsAndEntrys(tab3, "左キック", 3, 0, self.FootMotion_L_Stand["左キック"], 3, 1)
UI.createLabelsAndEntrys(tab3, "左キック", 3, 0, self.FootMotion_R_Stand["左キック"], 3, 2)
UI.createLabelsAndEntrys(tab3, "右キック", 4, 0, self.FootMotion_L_Stand["右キック"], 4, 1)
UI.createLabelsAndEntrys(tab3, "右キック", 4, 0, self.FootMotion_R_Stand["右キック"], 4, 2)
行数:10行