代码: 全选
#!/usr/bin/env python
#-*-coding:utf-8-*-
# file: PyGTKButtonEvent.py
#
import pygtk
pygtk.require('2.0')
import gtk
class MyWindow():
def __init__(self,title,width,height):
self.window=gtk.Window()
self.window.set_title(title)
self.window.set_default_size(width,height)
self.window.connect('destroy',lambda q: gtk.main_quit())
hbox=gtk.HBox(False,20)
self.button1=gtk.Button('Button1')
self.button2=gtk.Button('Button2')
self.button1.connect('clicked',self.OnButton,'Button1')
self.button2.connect('clicked',self.OnButton,'Button2')
hbox.pack_start(self.button1)
hbox.pack_start(self.button2)
self.window.add(hbox)
hbox.show()
self.button1.show()
self.button2.show()
self.window.show()
def main(self):
gtk.main()
def Onbutton1(self,widget,data):
self.button2.set_label('Quit')
def OnButton2(self,widget,data):
gtk.main_quit()
window=MyWindow('PyGTK',150,30)
window.main()
----------------------------------------------------------------------------------------
Traceback (most recent call last):
File "D:\employment\python\PyGTKButtonEvent.py", line 32, in <module>
window=MyWindow('PyGTK',150,30)
File "D:\employment\python\PyGTKButtonEvent.py", line 17, in __init__
self.button1.connect('clicked',self.OnButton,'Button1')
AttributeError: MyWindow instance has no attribute 'OnButton'