Pygtk:从容器中删除一个小部件并在以后重用它

时间:2011-04-10 21:27:41

标签: gtk pygtk

我正在使用自定义容器,我需要重新排序窗口小部件,但没有方法可以执行此操作。所以我尝试删除所有小部件并按顺序重新添加它们。

问题是这个效果不好,再次添加后我看不到小部件,我想发生的事情是当我删除小部件时它们变得未实现。

有没有办法删除小部件,以后再重用它?

3 个答案:

答案 0 :(得分:6)

pygtk docs提供了一些见解。

  

请注意,容器将拥有一个   对小部件的引用,这可能   是最后的参考;所以   从容器中删除小部件   可以导致该小部件被销毁。   如果你想再次使用widget,你   应该添加对它的引用。

<强> EDITS

我刚刚快速修改了pygtk的Hello World,以便在容器中添加/删除/重新排序小部件。这是有效的,因为button1是类的成员变量,它永远不会超出范围。

#!/usr/bin/env python

# example helloworld2.py

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld2:

    # Our new improved callback.  The data passed to this method
    # is printed to stdout.
    def callback_remove(self, widget, data):
    self.box1.remove(self.button1);

    def callback_add(self, widget, data):
        self.box1.pack_start(self.button1, True, True, 0)

    # another callback
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        # This is a new call, which just sets the title of our
        # new window to "Hello Buttons!"
        self.window.set_title("Hello Buttons!")

        # Here we just set a handler for delete_event that immediately
        # exits GTK.
        self.window.connect("delete_event", self.delete_event)

        # Sets the border width of the window.
        self.window.set_border_width(10)

        # We create a box to pack widgets into.  This is described in detail
        # in the "packing" section. The box is not really visible, it
        # is just used as a tool to arrange widgets.
        self.box1 = gtk.HBox(False, 0)

        # Put the box into the main window.
        self.window.add(self.box1)

        # Creates a new button with the label "Button 1".
        self.button1 = gtk.Button("Button 1")

        # Now when the button is clicked, we call the "callback" method
        # with a pointer to "button 1" as its argument
        self.button1.connect("clicked", self.callback_remove, "button 1")

        # Instead of add(), we pack this button into the invisible
        # box, which has been packed into the window.
        self.box1.pack_start(self.button1, True, True, 0)

        # Always remember this step, this tells GTK that our preparation for
        # this button is complete, and it can now be displayed.
        self.button1.show()

        # Do these same steps again to create a second button
        self.button2 = gtk.Button("Button 2")

        # Call the same callback method with a different argument,
        # passing a pointer to "button 2" instead.
        self.button2.connect("clicked", self.callback_add, "button 2")

        self.box1.pack_start(self.button2, True, True, 0)

        # The order in which we show the buttons is not really important, but I
        # recommend showing the window last, so it all pops up at once.
        self.button2.show()
        self.box1.show()
        self.window.show()

def main():
    gtk.main()

if __name__ == "__main__":
    hello = HelloWorld2()
    main()

答案 1 :(得分:1)

只需将窗口小部件的visibility属性设置为False,然后使用set_visible方法将其设置为True。

答案 2 :(得分:1)

不,他只需要widgrt.its对象不需要。然后可见不能使用。它只会隐藏graphicaly.but memmory仍未释放

删除功能就是答案