我試圖用gui創建我的第一個程序。我得到的錯誤如下:使用Python和Tkinter進行字符串格式化
異常在Tkinter的回調
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1487, in __call__
return self.func(*args)
File "/Volumes/Mac Storage/Python /letsbuildagui.py", line 7, in calcppp
labelresult=Label(window,text="The average price per portion is: " % mappp).push()
TypeError: not all arguments converted during string formatting
以下是我的代碼:
from tkinter import *
def calcppp():
lecost=float(cost.get())
leportions=float(portions.get())
mappp=lecost/leportions
labelresult=Label(window,text="The average price per portion is: " % mappp).push()
return
window = Tk()
window.geometry("500x500")
window.title("Price Per Portion")
cost=StringVar()
portions=StringVar()
welcome = Label(text='Please enter your total price of groceries \n and the amount of meals they yeilded to find out \n your average price per portion')
welcome.pack()
menubar = Menu(window)
button = Button(window, text="Calculate", command=calcppp)
button.place(x=200,y=450)
myCost=Entry(window,textvariable=cost).pack()
myPortions=Entry(window,textvariable=portions).pack()
window.config(menu=menubar)
window.mainloop()
謝謝你們,ocho88那定了! –