出租车的第一公里费用为8卢比,其余公里为5卢比。
根据这些信息得出方程式:
x = 10 (x is the distance travelled)
y = (10*5)+3 (y is the cost of travel)
使用以下信息制作简单但实用的gui:
代码:
import tkinter as tk
window = tk.Tk()
window.title("my maths project")
window.geometry("500x500")
#FUNCTIONS
def fare_calculater():
distance = int(entry_km.get())
fare = 3+(distance*5)
print(fare)
def fare_display():
showup = fare_calculater()
fare_display = tk.Text(master=window, height=10 , width=30)
fare_display.grid(column=0, row=5)
fare_display.insert(tk.NONE ,showup)
#LABEL
label_head = tk.Label(text="Hello User!. welcome to the app", font=("The New Roman", 25))
label_head.grid()
label_enter = tk.Label(text="Enter the distence commuted by the passanger below")
label_enter.grid(column=0, row=1)
#ENTRY
entry_km = tk.Entry()
entry_km.grid(column=0, row=2)
#button
button_submit = tk.Button(text="submit", bg="green", command=fare_display)
button_submit.grid(column=0, row=3)
这是错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\swadeshi\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line
1705, in __call__
return self.func(*args)
File "C:\Users\swadeshi\Desktop\math project 1.py", line 22, in fare_display
fare_display.insert(tk.NONE ,showup)
File "C:\Users\swadeshi\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line
3272, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: wrong # args: should be ".!text insert index chars ?tagList chars tagList ...?"
屏幕截图:
答案 0 :(得分:3)
您忘记了从fare_calculator函数返回票价(而是打印出来)。
用于插入的索引必须类似于tk.END或其他公认的索引。
例如
import tkinter as tk
window = tk.Tk()
window.title("my maths project")
window.geometry("500x500")
# FUNCTIONS
def fare_calculater():
distance = int(entry_km.get())
fare = 3 + (distance * 5)
print(fare)
return fare
def fare_display():
showup = str(fare_calculater())
fare_display = tk.Text(master=window, height=10, width=30)
fare_display.grid(column=0, row=5)
fare_display.insert(tk.END, showup)
# LABEL
label_head = tk.Label(text="Hello User!. welcome to the app", font=("The New Roman", 25))
label_head.grid()
label_enter = tk.Label(text="Enter the distence commuted by the passanger below")
label_enter.grid(column=0, row=1)
# ENTRY
entry_km = tk.Entry()
entry_km.grid(column=0, row=2)
# button
button_submit = tk.Button(text="submit", bg="green", command=fare_display)
button_submit.grid(column=0, row=3)
window.mainloop()