2005年09月17日 星期六 00:58
最近在学pygtk,看了些教程,其中有个例程有些疑问,请大家解惑。 例程代码如下: #!/usr/bin/env python # example-start buttons buttons.py import pygtk pygtk.require('2.0') import gtk # Create a new hbox with an image and a label packed into it # and return the box. def xpm_label_box(parent, xpm_filename, label_text): # Create box for xpm and label box1 = gtk.HBox(False, 0) box1.set_border_width(2) # Now on to the image stuff. image = gtk.Image() image.set_from_file(xpm_filename) # Create a label for the button label = gtk.Label(label_text) # Pack the pixmap and label into the box box1.pack_start(image, False, False, 3) box1.pack_start(label, False, False, 3) image.show() label.show() return box1 class Buttons: # Our usual callback method def call_back(self, widget, data=None): print "Hello again - %s was pressed" % data def __init__(self): # Create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_title("Image's Buttons!") # It's a good idea to do this for all windows. self.window.connect("destroy", lambda wid: gtk.main_quit()) self.window.connect("delete_event", lambda a1, a2: gtk.main_quit()) # Sets the border width of the window. self.window.set_border_width(10) # Create a new button button = gtk.Button() # Connect the "clicked" signal of the button to our callback button.connect("clicked", self.call_back, "cool button") # This calls our box creating function box1 = xpm_label_box(self.window, "info.xpm", "cool button") # Pack and show all our widgets button.add(box1) box1.show() button.show() self.window.add(button) self.window.show() def main(): gtk.main() return 0 if __name__ == "__main__": Buttons() main() ###########################END self.window.connect("destroy", lambda wid: gtk.main_quit()) self.window.connect("delete_event", lambda a1, a2: gtk.main_quit()) 这两处绑定信号处理函数时,为什么使用lambda函数呢?而且使用的参数个数也不一样,为什么不能直接绑定到gtk_main_quit()? 请大家给我讲讲window.connect()的详细用法,谢谢! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050917/630f9dc7/attachment.html
2005年09月17日 星期六 02:34
可以想象成这样 window.connect帮你定义的这个函数 def destory(wig): gtk.main_quit() def delete_event(a1,a2): gtk.main_quit() 当destory event发生的时候,wig会被这个event的caller的传入 destory(wig) 当delete_event event发生的时候,a1,a2会被这个event的caller的传入 delete_event(a1,a2) 因为传入什么值,几个值是caller已经决定好了的,而gtk_main_quit()不接受传入值,所以用了lambda > > self.window.connect("destroy", lambda wid: gtk.main_quit()) > self.window.connect("delete_event", lambda a1, a2: gtk.main_quit()) > 这两处绑定信号处理函数时,为什么使用lambda函数呢?而且使用的参数个数也不一样,为什么不能直接绑定到gtk_main_quit()? > 请大家给我讲讲window.connect()的详细用法,谢谢! > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050917/7da3f7cc/attachment.htm
Zeuux © 2025
京ICP备05028076号