动态更新 gtk.VBox

发布于 2024-10-16 16:07:05 字数 1242 浏览 9 评论 0原文

我经常使用这个网站来解决我在 Python 编程时遇到的小问题。这次,不知怎的,我找不到适合我情况的解决方案。所以,这是我的问题:

我想动态地将条目添加到 gtk.VBox 小部件。问题是它没有按照我想要的方式工作。我只有一个按钮,其操作是将附加小部件添加到 VBox。不幸的是,该小部件没有出现在窗口上。我想,我必须添加诸如重绘函数调用之类的东西,但我没有找到类似的东西。这是一个示例代码,显示了我的问题:

import gtk

class DynamicVbox:

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.close_application)
        self.window.set_size_request(400,320)
        #a hBox to put the button and the dynamic vBox
        hBox = gtk.HBox(False, 0)

        addButton = gtk.Button("add checkbox")
        addButton.connect("clicked", self.AddCheckButton)

        self.vBox = gtk.VBox(False, 0)
        self.vBox.pack_start(gtk.CheckButton("CheckButton"), True, True, 1)
        hBox.pack_start(self.vBox, True, True, 5)
        hBox.pack_end(addButton, False, False, 5)
        self.window.add(hBox)

        #start gtk
        self.window.show_all()
        gtk.main()

    def AddCheckButton(self, button):
        self.vBox.pack_start(gtk.CheckButton("CheckButton"), True, True, 1)
        print "adding checkbox..."

    def close_application(self, widget):
        gtk.main_quit()

 # run it

a = DynamicVbox()

感谢任何帮助。提前致谢。

I have been using this website pretty often in order to solve small issues that I have while programming in Python. This time, somehow I could not find a suitable solution for my situation. So, here is my problem:

I want to dynamically add entries to a gtk.VBox widget. The problem is that it doesn't work the way I want it to work. I simply have a button, whose action is to add an additional widget to a VBox. Unfortunately the widget doesn't appear on the window. I guess, I have to add something like a repaint function call, but I didn't find anything like that. Here is a sample code, showing my problem:

import gtk

class DynamicVbox:

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.close_application)
        self.window.set_size_request(400,320)
        #a hBox to put the button and the dynamic vBox
        hBox = gtk.HBox(False, 0)

        addButton = gtk.Button("add checkbox")
        addButton.connect("clicked", self.AddCheckButton)

        self.vBox = gtk.VBox(False, 0)
        self.vBox.pack_start(gtk.CheckButton("CheckButton"), True, True, 1)
        hBox.pack_start(self.vBox, True, True, 5)
        hBox.pack_end(addButton, False, False, 5)
        self.window.add(hBox)

        #start gtk
        self.window.show_all()
        gtk.main()

    def AddCheckButton(self, button):
        self.vBox.pack_start(gtk.CheckButton("CheckButton"), True, True, 1)
        print "adding checkbox..."

    def close_application(self, widget):
        gtk.main_quit()

 # run it

a = DynamicVbox()

A appreciate any help. Thanks in advance.

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

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

发布评论

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

评论(1

尐籹人 2024-10-23 16:07:05

新的检查按钮就在那里,但在您调用 show() 之前不可见:

def AddCheckButton(self, button):
    button = gtk.CheckButton("CheckButton")
    self.vBox.pack_start(button, True, True, 1)
    button.show()
    print "adding checkbox..."

The new check button is there, but not visible until you call show() on it:

def AddCheckButton(self, button):
    button = gtk.CheckButton("CheckButton")
    self.vBox.pack_start(button, True, True, 1)
    button.show()
    print "adding checkbox..."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文