有人問我來編輯我的代碼裏面的標籤,所以我決定把整個計算器腳本Tkinter的:如何改變程序本身
from tkinter import * global choice choice = 0 #Program def calculate(*event): if choice == 1: try: add1 = ccalc1.get() add2 = ccalc2.get() except: no = Label(app, text="You must use a number").grid(row=0, column=0) answ = add1 + add2 answer = Label(app, text = answ).grid(row=1, column=0) elif choice == 2: try: sub1 = ccalc1.get() sub2 = ccalc2.get() except: no = Label(app, text="You must use a number").grid(row=1, column=0) answ = sub1 - sub2 answer = Label(app, text = answ).grid(row=1, column=0) def choice2(): global choice choice = 2 #End Program #GUI #Window Info calc = Tk() calc.title("Calculator") calc.geometry("200x140") #End Window Info #Build Window app = Frame(calc) app.grid() ccalc1 = IntVar() ccalc2 = IntVar() #Widgets if choice == 0: welcome = Label(app, text="Select a choice") elif choice == 2: welcome.config(text="Subtraction") calcbox1 = Entry(app,textvariable=ccalc1) calcbox2 = Entry(app,textvariable=ccalc2) submit = Button(app, text="CALCULATE", command = calculate) welcome.grid(row=0,column=0) calcbox1.grid(row=2, column=0) calcbox2.grid(row=3, column=0) submit.grid(row=4, column=0) calc.bind('<Return>', calculate) #End Widgets #Menu menu=Menu(calc) #Operations filemenu = Menu(menu,tearoff=0) filemenu.add_command(label="Subtract", command = choice2) menu.add_cascade(label="Operations",menu=filemenu) calc.config(menu=menu) calc.mainloop() #End GUI
自己做錯了什麼是歡迎標籤文本不會隨之改變。
更新:我包括整個計算器代碼
任何幫助表示讚賞。
請仔細檢查您在這裏發佈的代碼。我試着運行它並得到了'IndentationError:unindent不匹配任何外部縮進級別'。 – Kevin 2014-09-18 20:04:28
更新爲包含整個計算器。它適用於我 – user3763447 2014-09-18 20:08:34
在菜單中選擇選項後,'選擇選項'會變爲'減法' – user3763447 2014-09-18 20:09:39