不明白为什么从函数插入结果导致此tkinter.TclError

时间:2019-10-15 17:31:57

标签: python python-3.x tkinter

出租车的第一公里费用为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 ...?"

屏幕截图:

screenshot of gui

1 个答案:

答案 0 :(得分:3)

  1. 您忘记了从fare_calculator函数返回票价(而是打印出来)。

  2. 用于插入的索引必须类似于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()