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

PyGtk中Vte.Terminal.feed_child()参数报错,求解决方案

解决Vte.Terminal.feed_child()参数不匹配的错误

错误原因

这个报错其实是VTE库版本差异搞的鬼!旧版本的VTE里,feed_child()方法确实需要你传入两个参数:要发送的命令字符串,以及字符串的长度。但在较新的VTE版本(比如0.50及以上)中,这个方法已经做了简化,它会自动计算传入字符串的长度,不再需要手动传入length参数了。你现在调用时传了3个参数(self, command, length),但当前版本的方法只接受2个(self, command),所以就触发了这个类型错误。

修复方案

只需要修改InputToTerm方法里的调用代码,去掉多余的length参数就行:

原错误代码:

self.terminal.feed_child(self.command, length)

修改后:

self.terminal.feed_child(self.command)

完整修改后的示例代码

from gi.repository import Gtk, Vte
from gi.repository import GLib
import os

class TheWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="inherited cell renderer")
        self.set_default_size(600, 300)
        self.terminal = Vte.Terminal()
        self.terminal.fork_command_full(
            Vte.PtyFlags.DEFAULT,
            os.environ['HOME'],
            ["/bin/sh"],
            [],
            GLib.SpawnFlags.DO_NOT_REAP_CHILD,
            None,
            None,
        )
        self.button = Gtk.Button("Do The Command")
        self.command = "echo \"Sending this command to a virtual terminal.\"\n"
        command = Gtk.Label("The command: "+self.command)
        self.button.connect("clicked", self.InputToTerm)
        
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        box.pack_start(self.button, False, True, 0)
        box.pack_start(command, False, True, 1)
        
        scroller = Gtk.ScrolledWindow()
        scroller.set_hexpand(True)
        scroller.set_vexpand(True)
        scroller.add(self.terminal)
        box.pack_start(scroller, False, True, 2)
        self.add(box)

    def InputToTerm(self, clicker):
        # 去掉了手动计算length的步骤,直接传入命令字符串
        self.terminal.feed_child(self.command)

win = TheWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

额外小提示

如果你想确认自己用的VTE版本,可以在终端里运行以下命令:

pkg-config --modversion vte-2.91  # 针对VTE3的命令

如果版本在0.50以上,就适用上面的修改方式。

内容的提问来源于stack exchange,提问作者Zeki Ahmet Bayar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 22:27:52