在編碼方面,我是一個非常初學者。我正在嘗試計算訂單的總和,但 python 一直告訴我它無法將字串轉換為浮點數。你能告訴我錯誤在哪里嗎?
import tkinter as tk
window = tk.Tk()
window.title("Cinema")
window.geometry("700x600")
def total_():
a= float(e1.get())*2
b= float(e2.get())*3
c= float(e3.get())*4
d= float(e5.get())*1
e= float(e4.get())*12
totl = a b c d e
tax= (totl * 0.13 0.13)
lblAns['text'] = "Your total is {}$".format(totl)
#Cinema name
Cine_nom = tk.Label(window)
Cine_nom['text'] = "Welcome to cinema !"
Cine_nom['font'] = "Arial 20 bold"
Cine_nom.place(x=350, y=20, anchor="center")
# Menu Creation
label1 = tk.Label(window,
text="Menu",
font="times 28 bold")
label1.place(x=520, y=70)
label2 = tk.Label(window, text="Popcorn (P) \
2$", font="times 18")
label2.place(x=450, y=120)
label3 = tk.Label(window, text="Popcorn (M) \
3$", font="times 18")
label3.place(x=450, y=150)
label4 = tk.Label(window, text="Popcorn (L) \
4$", font="times 18")
label4.place(x=450, y=180)
label5 = tk.Label(window, text="Burger Combo \
12$", font="times 18")
label5.place(x=450, y=210)
label6 = tk.Label(window, text="Soda \
1$", font="times 18")
label6.place(x=450, y=240)
# Entry Table
label9 = tk.Label(window, text="Select the items",
font="times 18")
label9.place(x=115, y=70)
label10 = tk.Label(window,
text="Popcorn (P)",
font="times 18")
label10.place(x=20, y=120)
e1 = tk.Entry(window)
e1.place(x=20, y=150)
label11 = tk.Label(window, text="Popcorn (M)",
font="times 18")
label11.place(x=20, y=200)
e2= tk.Entry(window)
e2.place(x=20, y=230)
label12 = tk.Label(window, text="Popcorn (L)",
font="times 18")
label12.place(x=20, y=280)
e3= tk.Entry(window)
e3.place(x=20, y=310)
label13 = tk.Label(window,
text="Burger Combo",
font="times 18")
label13.place(x=20, y=360)
e4 = tk.Entry(window)
e4.place(x=20, y=390)
label14 = tk.Label(window,
text="Soda",
font="times 18")
label14.place(x=20, y=420)
e5 = tk.Entry(window)
e5.place(x=20, y=450)
# Button for total
btnCalculate = tk.Button(window)
btnCalculate['text'] = "Calculate your bill"
btnCalculate['font'] = "Arial 15"
btnCalculate['command'] = total_
btnCalculate.place(x=30, y=480)
lblAns = tk.Label(window)
lblAns['text'] = ""
lblAns.place(x=40, y=700)
window.mainloop()
uj5u.com熱心網友回復:
當我向輸入框添加輸入時,它對我有用。我認為您只需要移動正在顯示的陳述句。我變了
lblAns.place(x=40, y=700)
到
lblAns.place(x=40, y=550)
然后我在五個輸入框(從上到下)中輸入了 1、2、3、4、5。它列印了Your total is 73.0$。鑒于這些是數量,我認為int比float.
要為每個條目設定默認值,請嘗試如下所示。
e1.insert(0, 0)
像下面這樣的東西也會起作用。
def total_():
a= float(e1.get() or 0)*2
b= float(e2.get() or 0)*3
c= float(e3.get() or 0)*4
d= float(e5.get() or 0)*1
e= float(e4.get() or 0)*12
totl = a b c d e
tax= (totl * 0.13 0.13)
lblAns['text'] = "Your total is {}$".format(totl)
uj5u.com熱心網友回復:
# use a debugger or print(type(variable)) to check types
def total_():
a = float(e1.get() * 2)
b = float(e2.get() * 3)
c = float(e3.get() * 4)
d = float(e5.get() * 1)
e = float(e4.get() * 12)
totl = float(a b c d e)
tax = float((totl * 0.13) 0.13)
lblAns['text'] = f"Your total is {totl}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/469143.html
