我正在使用自定義容器,我需要重新排序小部件,但沒有方法可以執行此操作。所以我試圖刪除所有的小部件,並再次按順序添加它們。Pygtk:從容器中移除一個小部件,稍後重用它
問題是這樣做效果不好,再次添加它們後我看不到這些小部件,我猜想發生了什麼事情是,當我刪除這些小部件時,它們變得不可實現。
是否有任何方法可以刪除一個小部件並在以後重用它?
我正在使用自定義容器,我需要重新排序小部件,但沒有方法可以執行此操作。所以我試圖刪除所有的小部件,並再次按順序添加它們。Pygtk:從容器中移除一個小部件,稍後重用它
問題是這樣做效果不好,再次添加它們後我看不到這些小部件,我猜想發生了什麼事情是,當我刪除這些小部件時,它們變得不可實現。
是否有任何方法可以刪除一個小部件並在以後重用它?
pygtk docs提供了一些洞察力。
注意容器將擁有 參考部件,並認爲這可能是 舉行的最後參考;因此從其容器 中刪除小部件可能會導致該小部件被銷燬。 如果您想再次使用小部件,您應該爲其添加引用。
編輯
我只是快速修改的PyGTK的世界您好添加/刪除/重新排序小部件的容器。這是可行的,因爲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()
就在小部件的可見性屬性設置爲False,並將其設置爲True後,隨着set_visible
方法。
沒有他只需要widgrt.its對象不need.then可見不得used.it只會隱藏graphicaly.but memmory仍然沒有公佈
刪除功能就是答案
是我讀到的信息,但我不知道是否可以手動添加對我的小部件的引用。小部件一直由python引用,而不是由GTK引用。 – pmoleri 2011-04-10 22:07:25
@pmoleri,我剛剛添加了一個快速而骯髒的例子。它可以很容易地移除並重新添加小部件到一個容器(一個HBox)。點擊「按鈕1」將刪除它,然後點擊「按鈕2」將它添加回容器的末端。我認爲「添加引用」只是意味着,你不能讓它超出範圍。 – Mark 2011-04-10 22:25:16