简体   繁体   English

pyGTK-gtk.gtk.Dialog中的条目不产生任何文本

[英]pyGTK - gtk.Entry in gtk.Dialog Yields no Text

In pyGTK (2.22 - version is very important ), I'm encountering the bug detailed below. 在pyGTK(2.22- 版本非常重要 )中,我遇到了以下错误。 I think it's a pyGTK issue, but I could be wrong and don't want to report a non-bug. 认为这是一个pyGTK问题,但我可能错了,也不想报告非bug。

Basically, I am extracting text from a gtk.Entry() using .get_text() , and this returns an empty string even with text in the widget. 基本上,我是使用.get_text()gtk.Entry()提取文本的,即使在小部件中包含文本,这也会返回一个空字符串。 Here is some relevant code (with NOOP definitions to make it runnable): 这是一些相关的代码(使用NOOP定义使其可运行):

import gtk

class Item: pass

def tofile(item): pass

# Described issues begin below

class ItemAddDialog:
    "A dialog used when adding a menu item"
    def __init__(self):
        self.dialog = gtk.Dialog(title="Adding menu item...", buttons=btns)
        self.fname, self.name, self.icon, self.exe, self.cats = [gtk.Entry() for i in range(5)]
        self.obs = (self.fname, self.name, self.icon, self.exe, self.cats)
        self._config()

    def _config(self):
        _ = self.dialog.vbox
        map(lambda x: _.pack_start(x, False, False, 0), self.obs)
        map(lambda x: x.show(), self.obs)
        map(lambda x: x[1].set_text(x[0]), zip(("Filename", "Name in Menu", "Icon", "Command", "Categories (; Delimited)"), self.obs))

    def run(self):
        r = self.dialog.run()
        self.dialog.destroy()
        print _quote(str(r))
        if (int(r) == 1): i = Item(self.fname.get_text(), self.name.get_text(), self.icon.get_text(), self.exe.get_text(), self.cats.get_text())
        print str(i)
        tofile(i)

destroy() will among other things cause the widget and its children to be unrealized, which means the entry loses its text. 除其他因素外, destroy()会导致小部件及其子级未实现,这意味着条目会丢失其文本。 Always read the state of a dialog (or any other widget) before destroying it. 销毁对话框之前,请务必先阅读其状态。

There are some other minor issues with your code: 您的代码还有其他一些小问题:

  1. For clarity, you should replace the maps with simple loops: 为了清楚起见,您应该使用简单的循环替换地图:

    map(lambda x: _.pack_start(x, False, False, 0), self.obs)

    for x in self.obs: _.pack_start(x, False, False)

    map(lambda x: x[1].set_text(x[0]), zip(("Filename", "Name in Menu", "Icon", "Command", "Categories (; Delimited)"), self.obs))

    for txt, x in zip(("Filename", "Name in Menu", "Icon", "Command", "Categories (; Delimited)"), self.obs)): x.set_text(txt)

  2. Instead of calling show on all the children, just call show_all on the parent (the dialog in this case). 除了在所有子节点上调用show ,只需在父节点上调用show_all (在这种情况下为对话框)。

  3. I don't think you have to cast the dialog result to an int. 我认为您不必将对话框结果转换为整数。 Also, magic numbers are bad. 而且,魔术数字是不好的。 Define a constant, or use a predefined one . 定义一个常数,或使用预定义的常数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM