本文介绍了动态更新gtk.VBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了解决使用Python编程时遇到的小问题,我经常使用该网站。这次,我无法以某种方式找到适合我的情况的解决方案。所以,这是我的问题:

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:

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

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.

推荐答案

新的复选按钮在那里,但是直到您调用 show( )

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..."

这篇关于动态更新gtk.VBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 07:52