2017-04-03 114 views
-1

我一直在玩tkinter一點,我發現你必須做一個def方法來使用按鈕。所以我想製作13個按鈕,因爲我想製作一個計算器,但我實際上不想製作13種非常相似的def方法。我試着做一個嵌套的def方法(我從來沒有真正做過),但是,從我試過的,它不會工作。我只是做錯了,或者僅僅是不可能的。如果不可能,除了大量的副本和大量的粘貼之外,還有其他方式可以大量生成def方法嗎?代碼如下:你可以做一個def方法,爲Python創建更多的def方法

from tkinter import * 
window=Tk() 


window.geometry("500x500") 
print("Restarting") 
user="" 
def oneb(): 
    global user 
    print("1",end="") 
    user+="1" 
def twob(): 
    global user 
    user+="2" 
    print("2",end="") 
def threeb(): 
    global user 
    user+="3" 
    print("3",end="") 
def fourb(): 
    global user 
    user+="4" 
    print("4",end="") 
def fiveb(): 
    global user 
    user+="5" 
    print("5",end="") 
def sixb(): 
    global user 
    user+="6" 
    print("6",end="") 
def sevenb(): 
    global user 
    user+="7" 
    print("7",end="") 
def eightb(): 
    global user 
    user+="8" 
    print("8",end="") 
def nineb(): 
    global user 
    user+="9" 
    print("9",end="") 
def zerob(): 
    global user 
    user+="0" 
    print("0",end="") 
def plusb(): 
    global user 
    user+="+" 
    print("+",end="") 
def minusb(): 
    global user 
    print("-",end = "") 
def equalb(): 
    global user 
    if "+" in user: 
     user=user.partition("+") 
     symbol="+" 
    elif "-" in user: 
     user=user.partition("-") 
     symbol="-" 
    else: 
     print("=",user) 
    num1=user[0] 
    num2=user[2] 
    num1=int(num1) 
    num2=int(num2) 
    if symbol=="+": 
     answer=num1+num2 
    else: 
     answer=num1-num2 
    answer=str(answer) 
    print("="+answer) 


heightb=5 
widthb=10 

#I know here I probably should've just made a def method. 
one=Button(window, text="1",command=oneb,height=heightb,width=widthb) 
one.grid(row=1,column=1) 

two=Button(window, text="2",command=twob,height=heightb,width=widthb) 
two.grid(row=1,column=2) 

three=Button(window, text="3",command=threeb,height=heightb,width=widthb) 
three.grid(row=1,column=3) 

four=Button(window, text="4",command=fourb,height=heightb,width=widthb) 
four.grid(row=2,column=1) 

five=Button(window, text="5",command=fiveb,height=heightb,width=widthb) 
five.grid(row=2,column=2) 

six=Button(window, text="6",command=sixb,height=heightb,width=widthb) 
six.grid(row=2,column=3) 

seven=Button(window, text="7",command=sevenb,height=heightb,width=widthb) 
seven.grid(row=3,column=1) 

eight=Button(window, text="8",command=eightb,height=heightb,width=widthb) 
eight.grid(row=3,column=2) 

nine=Button(window, text="9",command=nineb,height=heightb,width=widthb) 
nine.grid(row=3,column=3) 

zero=Button(window, text="0",command=zerob,height=heightb,width=widthb) 
zero.grid(row=4,column=2) 

plus=Button(window, text="+", command=plusb,height=heightb, width=widthb) 
plus.grid(row=2,column=4) 

minus=Button(window,text="-", command=minusb,height=heightb, width=widthb) 
minus.grid(row=1,column=4) 

equal=Button(window,text="=", command=equalb,height=heightb, width=widthb) 
equal.grid(row=3,column=4) 



mainloop() 
#button location var.grid(row=x,column=x) 
+1

您能否顯示您的代碼,以及您試圖用每個按鈕完成的操作? –

+0

另外,考慮使用'lambda's而不是你的tkinter按鈕的函數。 –

+0

您是否嘗試過使用接受附加參數來標識按鈕的方法?然後你可以保存你的通用代碼並按照按下的按鈕在一個def中執行特殊代碼。 – NineTails

回答

1

您可以創建一個返回函數的函數。

例如,

def build_button(number): 
    def button(): 
     global user 
     user += str(number) 
     print(number, end="") 
    return button 

oneb = build_button(1) 
twob = build_button(2) 
# ... 

這應該是功能上等同於你有上述[數字] B類函數。