You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法将LineEdit文本传递至绑定函数的技术问题

问题分析

你的代码核心问题在于确认按钮connect时传递的是变量的即时值,而非动态引用:

  • 执行accept_bt.connect("pressed", ..., [dict, temp_text_key, temp_text_value])时,temp_text_key和temp_text_value是空字符串,这个值会被固定下来。后续输入文本更新变量时,回调函数收到的还是最初的空值。
  • 全局临时变量temp_text_key/temp_text_value还存在多弹窗数据覆盖的风险,比如同时打开多个窗口时,输入内容会互相干扰。
解决方案(推荐方案,无需全局变量)

直接在确认按钮的回调中获取LineEdit的当前文本,避免依赖全局变量和connect传值的陷阱:

修改代码步骤:

  1. 绑定确认按钮时,把le_key和le_value作为节点引用传递:
# 替换原来的accept_bt.connect行
accept_bt.connect("pressed", self, "_on_add_new_property_confirmation", [dict, le_key, le_value])
  1. 更新回调函数,直接从LineEdit节点获取实时文本:
func _on_add_new_property_confirmation(dict: Dictionary, le_key: LineEdit, le_value: LineEdit):
    # 获取当前输入的文本,同时去除首尾空格
    var prop_name = le_key.text.strip_edges()
    var prop_value = le_value.text.strip_edges()
    
    if prop_name == "" or prop_value == "":
        send_message_to_console("Neither of the two can be empty", 3)
        return
    if add_dictionary_flag:
        dict[prop_name] = {
            prop_value: {
                "id": get_free_local_id_for_dict(dict)
            }
        }
    elif !add_dictionary_flag:
        if keywords_for_dictionaries.has(prop_name):
            send_message_to_console("Only Dictionary items can become %s" % prop_name)
        dict[prop_name] = prop_value
    
    # 清空输入框(可选)
    le_key.text = ""
    le_value.text = ""
  1. 可以删除全局的temp_text_key/temp_text_value变量,以及对应的_on_add_new_prop_text_entered函数,简化代码结构。
备选方案(保留全局变量)

如果一定要用全局临时变量,不要在connect时传递变量值,而是在回调中直接读取全局变量:

  1. 修改connect语句,只传递dict:
accept_bt.connect("pressed", self, "_on_add_new_property_confirmation", [dict])
  1. 调整回调函数:
func _on_add_new_property_confirmation(dict: Dictionary):
    var prop_name = temp_text_key.strip_edges()
    var prop_value = temp_text_value.strip_edges()
    
    # 后续逻辑和原来一致
    if prop_name == "" or prop_value == "":
        send_message_to_console("Neither of the two can be empty", 3)
        return
    # ... 其余代码不变
    
    temp_text_key = ""
    temp_text_value = ""

此方案仍存在多窗口数据冲突的风险,不推荐使用。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 17:45:47