【问题标题】:Don't understand why inserting result from a function is causing this tkinter.TclError不明白为什么从函数插入结果会导致此 tkinter.TclError
【发布时间】:2019-10-16 02:44:01
【问题描述】:

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

截图:

【问题讨论】:

  • 为什么变量fare_display = tk.Text(...和函数使用同名:def fare_display():
  • 你认为fare_display.insert(tk.NONE ,showup) 在做什么?第一个参数是一个索引,你认为索引tk.NONE代表什么?

标签: python python-3.x tkinter


【解决方案1】:
  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()

【讨论】:

  • 谢谢......我真的很感激先生......我不得不把这个项目交给我的数学老师,证明她可以给我分数,这是提交的最后一天
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2014-11-01
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多