2

我正在尝试使用 pygame 制作网络浏览器。我正在使用 PGU 进行 html 渲染。当我访问简单的网站时,它运行良好,例如example.com,但是当我尝试加载使用 html 表单的更复杂的东西时,例如google,我收到此错误:

UnboundLocalError: local variable 'e' referenced before assignment

我查看了 PGU html 渲染文件,发现了这段代码:

def start_input(self,attrs):
    r = self.attrs_to_map(attrs)
    params = self.map_to_params(r) #why bother
    #params = {}

    type_,name,value = r.get('type','text'),r.get('name',None),r.get('value',None)
    f = self.form
    if type_ == 'text':
        e = gui.Input(**params)
        self.map_to_connects(e,r)
        self.item.add(e)
    elif type_ == 'radio':
        if name not in f.groups:
            f.groups[name] = gui.Group(name=name)
        g = f.groups[name]
        del params['name']
        e = gui.Radio(group=g,**params)
        self.map_to_connects(e,r)
        self.item.add(e)
        if 'checked' in r: g.value = value
    elif type_ == 'checkbox':
        if name not in f.groups:
            f.groups[name] = gui.Group(name=name)
        g = f.groups[name]
        del params['name']
        e = gui.Checkbox(group=g,**params)
        self.map_to_connects(e,r)
        self.item.add(e)
        if 'checked' in r: g.value = value

    elif type_ == 'button':
        e = gui.Button(**params)
        self.map_to_connects(e,r)
        self.item.add(e)
    elif type_ == 'submit':
        e = gui.Button(**params)
        self.map_to_connects(e,r)
        self.item.add(e)
    elif type_ == 'file':
        e = gui.Input(**params)
        self.map_to_connects(e,r)
        self.item.add(e)
        b = gui.Button(value='Browse...')
        self.item.add(b)
        def _browse(value):
            d = gui.FileDialog();
            d.connect(gui.CHANGE,gui.action_setvalue,(d,e))
            d.open();
        b.connect(gui.CLICK,_browse,None)

    self._locals[r.get('id',None)] = e

我在最后一行得到了错误,因为 e 没有定义。我猜这是因为检查输入类型并创建 e 变量的 if 语句与任何内容都不匹配。我添加了一行来打印 _type 变量,当我尝试googleapple时,我得到了“隐藏” 。有没有办法用 PGU 渲染具有“隐藏”类型的表单项?

编辑:
如果我在 if 语句中添加一个部分来检查 type_ 是否等于“隐藏”,我会在里面放什么?
编辑 2:
我意识到 PGU 的 html 渲染不是很好(它甚至显示 javascript 代码),所以我想知道是否有任何其他方式在 pygame 窗口中渲染 html。

4

1 回答 1

1

我认为可以将 PyGame 嵌入 PyQT 窗口中。不过,这更像是一种解决方法,而不是一种优雅的解决方案。

于 2010-06-13T05:47:59.987 回答