gtk.Builder() 和多个空地文件中断

发布于 2024-08-18 15:09:53 字数 1032 浏览 3 评论 0原文

我有一个glade gui,我也想使用glade 文件插入另一个对象。

当我按照下面的方式执行此操作时(这本质上就是我正在做的事情),整个应用程序挂起,self.show() 并将 CPU 最大化至 100%。如果我将 init() 的第一行替换为 self.builder = gtk.Builder() 然后应用程序运行,我可以设置小部件,即:设置条目的内容,设置和更改值组合框。但我无法响应信号,按钮单击永远不会调用处理程序。

在实际代码中,对象二被设置为笔记本中的页面,并且我有多个其他页面,gtk.main()位于拥有笔记本的对象中。所有这些都按预期工作,只是对象失败了。

有什么线索吗?我尝试为每个小部件调用 self.builder.connect_signals() 但它仍然无法注意到它们。

class one(gtk.VBox):
 def __init__(self, builder):
        gtk.VBox.__init__(self)
        self.builder = builder  # if this is self.builder = gtk.Builder() app runs but widget signals go missing.
        self.builder.add_from_file("ui_for_one.glade")
     self.show()  # Endless loop here?

class two(object):  # This is the page in a notebook.   
 def __init__(self):
  self.builder = gtk.Builder()
  self.builder.add_from_file("ui_for_two.glade")
  self.some_container = self.builder.get_object("some_container")
  self.one = one(self.builder)
  self.some_container.pack_start(self.one, False, False)

I have a glade gui, and I want to insert another object using a glade file as well.

When I do it as bellow (this is essentially what I am doing) the whole app hangs and the self.show() and maxes out the CPU at 100%. If I replace the first line of one's init() with self.builder = gtk.Builder() then the app runs, I can set widgets, ie: set contents of entry's, set and change the values of comboboxes. But I cant respond to signals, button clicks never call the handler.

In the real code the object two is set as a page in a note book, and I have multiple other pages, the gtk.main() is in the object that owns the notebook. All these work as expected, it's just the object one that fails.

Any clues? I have tried calling self.builder.connect_signals() for every widget but it still fails to notice them.

class one(gtk.VBox):
 def __init__(self, builder):
        gtk.VBox.__init__(self)
        self.builder = builder  # if this is self.builder = gtk.Builder() app runs but widget signals go missing.
        self.builder.add_from_file("ui_for_one.glade")
     self.show()  # Endless loop here?

class two(object):  # This is the page in a notebook.   
 def __init__(self):
  self.builder = gtk.Builder()
  self.builder.add_from_file("ui_for_two.glade")
  self.some_container = self.builder.get_object("some_container")
  self.one = one(self.builder)
  self.some_container.pack_start(self.one, False, False)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-08-25 15:09:53

在两个类中使用相同的 gtk.Builder 对象是否有充分的理由?
这可能是您的问题的原因。在您的 one 类中,您加载了一个空地文件,但您从未对其小部件执行任何操作。像这样的东西应该有效:

class one(gtk.VBox):

  def __init__(self):
    gtk.VBox.__init__(self)
    self.builder = gtk.Builder()
    self.builder.add_from_file("ui_for_one.glade")
    some_widget = self.builder.get_object("some_widget")
    self.add(some_widget)
    self.builder.connect_signals(self)
    # No reason to call self.show() here, that should be done manually.

  #Your callback functions here

class two(object):  # This is the page in a notebook.   

  def __init__(self):
    self.builder = gtk.Builder()
    self.builder.add_from_file("ui_for_two.glade")
    self.some_container = self.builder.get_object("some_container")
    self.one = one()
    self.some_container.pack_start(self.one, False, False)
    self.some_container.show_all() #recursively show some_container and all its child widgets

    self.builder.connect_signals(self)

有关更多信息,请查看这些Glade 教程

Is there a good reason for using the same gtk.Builder object in two classes?
This might be the cause of your problem. In your one class, you load a glade file but you never do anything with its widgets. Something like this should work:

class one(gtk.VBox):

  def __init__(self):
    gtk.VBox.__init__(self)
    self.builder = gtk.Builder()
    self.builder.add_from_file("ui_for_one.glade")
    some_widget = self.builder.get_object("some_widget")
    self.add(some_widget)
    self.builder.connect_signals(self)
    # No reason to call self.show() here, that should be done manually.

  #Your callback functions here

class two(object):  # This is the page in a notebook.   

  def __init__(self):
    self.builder = gtk.Builder()
    self.builder.add_from_file("ui_for_two.glade")
    self.some_container = self.builder.get_object("some_container")
    self.one = one()
    self.some_container.pack_start(self.one, False, False)
    self.some_container.show_all() #recursively show some_container and all its child widgets

    self.builder.connect_signals(self)

For more info, check out these Glade tutorials.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文