【发布时间】:2020-06-25 18:57:01
【问题描述】:
我一直在制作一个响应用户输入的程序。当我运行它时,我的代码不会将响应插入到文本小部件中。我得到错误:
TypeError: insert() 缺少 1 个必需的位置参数:'chars'
我的代码是:
global stuff
stuff = open("Dictionary.txt", "r")
global contents
contents = stuff.read()
stuff.close()
from tkinter import *
dictionary = {"chungus": "Come at me chungus ... you wanna go?",
"hi": "It's good to see you!", "bot": "No - you're the BOT"}
def output():
TT = entry.get()
text.delete(0.0, END)
try:
meaning = dictionary[TT]
except:
meaning = "We do not have a reply for this yet..."
text.insert(meaning)
def words():
TT = (contents)
text.delete(0.0, END)
meaning = (TT)
text.insert(END, meaning)
global window
window = Tk()
window.title("WFR")
label1 = Label(window, text="Enter stuff for reply (No caps): ")
label1.grid(row=0, column=0, sticky=W)
entry = Entry(window, width=35, bg="light green")
entry.grid(row=1, column=0, sticky=W)
button1 = Button(window, text="SUBMIT", width=8, command=output)
button1.grid(row=3, column=0, sticky=W)
text = Text(window, width=60, height=20, wrap=WORD, background="yellow")
text.grid(row=2, column=0, sticky=W)
menubar = Menu(window)
firstmenu = Menu(menubar, tearoff=0)
firstmenu.add_command(label="Type What?", command=words)
menubar.add_cascade(label="Options", menu=firstmenu)
window.config(menu=menubar)
window.mainloop()
我错过了什么吗?
【问题讨论】:
-
注释掉三个 stuff 语句(2,4 和 5)并在 19 处将
END添加到text.insert(END, meaning)- 根据 PNX 下面的回答将起作用。从 dict 输入值正确响应,并在出错时得到无回复消息。 -
请分享整个错误信息。不要使用
import *,这不是一个好主意。 -
阅读stackoverflow.com/help/minimal-reproducible-example 中的建议。它非常适合调试以及在失败时提出更好的问题。