我是python的新手,我正在尝试为计算停车成本的程序创建gui。运行代码时没有错误,但程序不会生成带有计算成本的标签。如果字母C,D或V没有输入到输入框中,我还希望它生成一条要求输入有效代码的消息。任何有关我出错的帮助将不胜感激!我知道布局看起来并不好看,我现在要回去修复标签的位置,然后更多地获得我正在努力解决的代码工作的核心基础知识。
-- coding: cp1252 --
import Tkinter as tk
import re
from functools import partial
def Vehicle(label_result, n1, n2):
Vehicle = Vehicletypeentrytext.get()
Vehicle_upper = Vehicle.upper()
if not re.match("^[C,V,D]*$", Vehicle_upper):
label_result.config(text="Please input valid vehicle code")
return
elif Vehicle_upper=='C':
def carcost(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = (int(num2)-int(num1))+1
label_result.config(text="Cost for Parking is %d" % result)
return
elif Vehicle_upper=='V':
def vancost(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = (int(num1)-int(num2))+2
label_result.config(text="Cost for Parking is%d" % result)
return
elif Vehicle_upper=='D':
def disabledcost(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = (int(num1)-int(num2))
label_result.config(text="Cost for Parking is%d" % result)
return
root = tk.Tk()
root.geometry('800x600')
root.title('Simple Calculator')
number1 = tk.StringVar()
number2 = tk.StringVar()
Title = tk.Label(root, text="SHORTSTAY CARPARK PAYMENT").grid(row=0, column=2)
Price = tk.Label(root, text = "1 hour = $2.00 / $3.00 / $1.00\n2 hour = $3.00 / $4.00 / $2.00\n3 hour = $4.00 / $5.00 / $3.00\n4 hour = $5.00 / $6.00 / $4.00\n").grid(row=1, column=0)
Vehicletypeentrytext = tk.StringVar()
Vehicletypelabel = tk.Label(root, text = 'Enter your Vehicle type:\nC for Car\tV for Van\tD for Disabled driver').grid(row=2, column=3)
Vehicletypeentry = tk.Entry(root, textvariable=Vehicletypeentrytext).grid(row=4, column=3)
labelNum1 = tk.Label(root, text="Enter Time in:").grid(row=5, column=0)
labelNum2 = tk.Label(root, text="Enter Time Out:").grid(row=6, column=0)
entryNum1 = tk.Entry(root, textvariable=number1).grid(row=5, column=2)
entryNum2 = tk.Entry(root, textvariable=number2).grid(row=6, column=2)
labelResult = tk.Label(root)
labelResult.grid(row=7, column=2)
Vehicle = partial(Vehicle, labelResult, number1, number2)
buttonCal = tk.Button(root, text="Calculate", command=Vehicle).grid(row=7, column=0)
root.mainloop()
答案 0 :(得分:0)
您正在调用def Vehicle(label_result, n1, n2)
内的函数,这些函数不会向您的脚本添加任何内容,因此会阻止结果显示为Label
。无需在if elif
语句中创建函数。检查我的功能def Vehicle(label_result, n1, n2)
import Tkinter as tk
import re
from functools import partial
def Vehicle(label_result, n1, n2):
Vehicle = Vehicletypeentrytext.get()
Vehicle_upper = Vehicle.upper()
if not re.match("^[C,V,D]*$", Vehicle_upper):
label_result.config(text="Please input valid vehicle code")
return
elif Vehicle_upper == 'C':
num1 = (n1.get())
num2 = (n2.get())
result = (int(num2) - int(num1)) + 1
label_result.config(text="Cost for Parking is %d" % result)
return
elif Vehicle_upper == 'V':
num1 = (n1.get())
num2 = (n2.get())
result = (int(num1) - int(num2)) + 2
label_result.config(text="Cost for Parking is%d" % result)
return
elif Vehicle_upper == 'D':
num1 = (n1.get())
num2 = (n2.get())
result = (int(num1) - int(num2))
label_result.config(text="Cost for Parking is%d" % result)
return
root = tk.Tk()
root.geometry('800x600')
root.title('Simple Calculator')
number1 = tk.StringVar()
number2 = tk.StringVar()
Title = tk.Label(root, text="SHORTSTAY CARPARK PAYMENT").grid(row=0, column=2)
Price = tk.Label(root, text = "1 hour = $2.00 / $3.00 / $1.00\n2 hour = $3.00 / $4.00 / $2.00\n3 hour = $4.00 / $5.00 / $3.00\n4 hour = $5.00 / $6.00 / $4.00\n").grid(row=1, column=0)
Vehicletypeentrytext = tk.StringVar()
Vehicletypelabel = tk.Label(root, text = 'Enter your Vehicle type:\nC for Car\tV for Van\tD for Disabled driver').grid(row=2, column=3)
Vehicletypeentry = tk.Entry(root, textvariable=Vehicletypeentrytext).grid(row=4, column=3)
labelNum1 = tk.Label(root, text="Enter Time in:").grid(row=5, column=0)
labelNum2 = tk.Label(root, text="Enter Time Out:").grid(row=6, column=0)
entryNum1 = tk.Entry(root, textvariable=number1).grid(row=5, column=2)
entryNum2 = tk.Entry(root, textvariable=number2).grid(row=6, column=2)
labelResult = tk.Label(root)
labelResult.grid(row=7, column=2)
Vehicle = partial(Vehicle, labelResult, number1, number2)
buttonCal = tk.Button(root, text="Calculate", command=Vehicle).grid(row=7,
column=0)
root.mainloop()