2011-04-10 3 views
7

위젯을 재정렬해야하는 곳에서 사용자 정의 컨테이너를 사용하고 있지만이를 수행 할 방법이 없습니다. 그래서 모든 위젯을 제거하고 순서대로 다시 추가하려고했습니다.Pygtk : 컨테이너에서 위젯을 제거하고 나중에 다시 사용하십시오.

문제는 이것이 잘 작동하지 않는다는 것인데, 다시 추가 한 후에 위젯을 볼 수 없다는 것입니다. 무슨 일이 일어 났는지는 내가 위젯을 제거 할 때 실현되지 않는다는 것입니다.

위젯을 제거하여 나중에 다시 사용할 수있는 방법이 있습니까?

답변

6

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() 
+0

예 내가 그 정보를 읽을 대답은 해제되지 graphicaly.but memmory을 숨길 만합니다 안 개체를 widgrt.its 없습니다 하지만 내 위젯에 수동으로 참조를 추가 할 수 있는지 여부는 알 수 없습니다. 위젯은 파이썬이 참조하지만 GTK는 참조하지 않습니다. – pmoleri

+0

@pmoleri, 방금 빠르고 더러운 예제를 추가했습니다. 위젯을 컨테이너 (HBox)에 쉽게 추가하고 다시 추가 할 수 있습니다. '버튼 1'을 클릭하면 제거되고 '버튼 2'를 클릭하면 컨테이너 끝에 다시 추가됩니다. 제 생각에 "참조 추가"는 단지 범위를 벗어나게 만들 수 없다는 의미입니다. – Mark

1

그냥 False로 위젯의 가시성 속성을 설정하고 set_visible 방법, 나중에 True로 설정합니다.

1

그가 필요없는 만 need.then 표시되지는 used.it 여전히

제거 기능이

관련 문제