gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:54:08 26 4
gpt4 key购买 nike

第一公里的出租车费用为 8 卢比,其余一公里为 5 卢比。
从这些信息中得出一个等式:

x = 10 (x is the distance travelled) 
y = (10*5)+3 (y is the cost of travel)

使用这些信息制作一个简单但实​​用的图形用户界面:

代码:

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. 您忘记从 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()

关于python - 不明白为什么从函数插入结果会导致此 tkinter.TclError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com