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

Racket GUI代码运行报错求助:text%实例化出现未使用参数问题

Racket GUI text% 初始化错误修复方案
  • 核心问题:text% 类本身不接受parent和label作为初始化参数。它是纯文本编辑对象,需要嵌入到editor-canvas%容器中才能在GUI窗口里显示,不能像普通带父控件的组件那样直接实例化。

  • 修复步骤:

    1. 单独实例化text%,不需要传递parent或label:
      (define route-text (new text%))
      
    2. 创建editor-canvas%作为text%的载体,给editor-canvas%指定父组件(比如窗口面板),并通过editor参数关联刚才的text%对象:
      (new editor-canvas%
           [parent main-panel]
           [editor route-text])
      
    3. 如果需要显示路由相关的标签,用独立的message%组件实现,不要给text%附加label:
      (new message% [parent main-panel] [label "示例路由:"])
      
  • 适配伦敦交通规划器的完整示例片段:

    (require racket/gui)
    
    ; 创建主窗口
    (define main-frame (new frame% [label "伦敦交通规划器"] [width 600] [height 400]))
    ; 创建垂直布局面板
    (define main-panel (new vertical-panel% [parent main-frame] [spacing 10] [margin 10]))
    
    ; 添加路由标题标签
    (new message% [parent main-panel] [label "硬编码示例路由:"])
    
    ; 正确创建文本组件并嵌入到画布
    (define route-content (new text%))
    (send route-content insert "伦敦桥 → 滑铁卢站 → 国王十字车站 → 帕丁顿站")
    (new editor-canvas% [parent main-panel] [editor route-content] [min-height 120])
    
    ; 显示窗口
    (send main-frame show #t)
    
  • 排查额外问题:如果移除parent和label后仍报错,检查所有new text%的调用,确保只传递它支持的参数(比如auto-wrap?、font、foreground等),同时确认text%已经通过editor-canvas%正确挂载到GUI组件树中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 08:31:06