You need to enable JavaScript to run this app. 如何解决Tkinter调用send()时报错缺少必填位置参数event的问题
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Tkinter调用send()时报错缺少必填位置参数event的问题

问题原因

你绑定回车键触发send()时,Tkinter的事件绑定机制会自动传入事件对象作为参数;但点击Send按钮触发send()时,按钮的command调用不会传入任何参数,而你定义的send()要求必须传入event参数,就触发了参数缺失的报错。

解决方案

无需修改现有绑定逻辑,只要把event参数改为可选参数,默认值设为None即可,适配两种触发场景:
把函数定义

def send(event):

修改为

def send(event=None):

补充修复

你当前代码还存在两个可运行问题,可同步修改:

  1. 你代码中判断if msg != ''但未定义msg变量,需要在send()函数开头加入获取输入框内容的逻辑:
msg = EntryBox.get("1.0", END).strip()
  1. 消息发送完成后可以清空输入框,方便下一次输入,在send()函数末尾加入:
EntryBox.delete("1.0", END)

修改后的完整send()函数示例

def send(event=None):
    msg = EntryBox.get("1.0", END).strip()
    if msg != '':
        ChatLog.config(state=NORMAL)
        frame1 = Frame(ChatLog, bg="#79C7C5")
        Message(
            frame1,
            text=msg, # 此处为用户输入的消息内容
            font=(
                "Arial",
                10),
            bg="#79C7C5",
            fg='#ffffff').grid(
                row=0,
                column=0,
                sticky="w",
                padx=3,
            pady=5)
        Label(
            frame1,
            text=datetime.now().strftime("%H:%M"),
            font=(
                "Arial",
                7),
            bg="#79C7C5",
            fg='#ffffff').grid(
                row=1,
                column=0,
            sticky="e")
        frame2 = Frame(ChatLog, bg="#878787")
        Message(
            frame2,
            text="机器人回复内容", # 此处后续可替换为聊天机器人返回的结果
            font=(
                "Arial",
                10),
            bg="#878787",
            fg='#ffffff').grid(
                row=0,
                column=0,
                sticky="w",
                padx=3,
            pady=5)
        Label(
            frame2,
            text=datetime.now().strftime("%H:%M"),
            font=(
                "Arial",
                7),
            bg="#878787",
            fg='#ffffff').grid(
                row=1,
                column=0,
            sticky="e")

        ChatLog.insert('end', '\n ')
        ChatLog.window_create('end', window=frame1)
        ChatLog.tag_add("tag-right", "end-1c linestart", "end-1c lineend")
        ChatLog.insert('end', '\n ')
        ChatLog.window_create('end', window=frame2)
        ChatLog.config(state=DISABLED)
        EntryBox.delete("1.0", END)

内容的提问来源于stack exchange,提问作者Subhasish Bora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 18:36:10