亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: [有趣的Python 6] PyGtk中窗口的事件處理 [打印本頁]

作者: sakulagi    時(shí)間: 2005-06-08 08:31
標(biāo)題: [有趣的Python 6] PyGtk中窗口的事件處理
還記得我們上次的PyGtk的程序么。我們顯示了一個(gè)光禿禿的窗口。而且不能關(guān)閉!呵呵。這樣太不象話了。我們來看一個(gè)復(fù)雜一點(diǎn)的例子,順便看一下PyGtk的事件處理機(jī)制:

  1. import gtk

  2. class Shell:
  3.     def __init__(self):
  4.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  5.         self.window.connect("destroy", self.destroy_event, None)
  6.         self.window.connect("delete_event", self.delete_event, None)
  7.         self.initContainer()
  8.         self.window.add(self.container)
  9.         self.container.show()
  10.         self.window.show()

  11.     def initContainer(self):
  12.         self.helloButton = gtk.Button("Hello World")
  13.         self.helloButton.connect("clicked", self.hello, None)

  14.         self.quitButton = gtk.Button("Quit")
  15.         self.quitButton.connect_object("clicked", gtk.Widget.destroy, self.window)
  16.         self.container = gtk.HBox()
  17.         self.container.add(self.helloButton)
  18.         self.container.add(self.quitButton)
  19.         self.helloButton.show()
  20.         self.quitButton.show()
  21.         return self.container

  22.     def delete_event(self, widget, event, data = None):
  23.         print "delete event caught"
  24.         return False

  25.     def destroy_event(self, widget, event, data = None):
  26.         print "destroy event caught"
  27.         gtk.main_quit()

  28.     def hello(self, widget, event, data=None):
  29.         print "Hello PyGtk World!"

  30.     def main(self):
  31.         gtk.main()

  32. print __name__
  33. if __name__ == "__main__":
  34.     shell = Shell()
  35.     shell.main()
復(fù)制代碼

如果有Gtk編程經(jīng)驗(yàn)的話,可以很容易讀懂這段代碼。其實(shí)Gtk的機(jī)制很簡單,就是在某個(gè)widget上,為某一個(gè)特定的事件注冊(cè)一個(gè)callback函數(shù)。當(dāng)事件發(fā)生時(shí),對(duì)應(yīng)的callback事件會(huì)被調(diào)用。
對(duì)于delete_event,這個(gè)事件通常是由window manager來發(fā)出,點(diǎn)擊窗口右上教的"X"就會(huì)觸發(fā)這個(gè)事件。
對(duì)于connect_object(event, callback, widget)這個(gè)函數(shù),其效果是這樣的:

  1. callback(widget)
復(fù)制代碼

會(huì)被調(diào)用。
在本例子里,gtk.Widget.destroy(self.window)會(huì)被調(diào)用。而gtk.Widget.destroy(widget)的作用是向widget發(fā)出destroy信號(hào)。如果widget注冊(cè)了這個(gè)signal的callback,本例中是Shell.destroy_event()函數(shù)。結(jié)果是程序的退出。
作者: dragonII    時(shí)間: 2005-07-06 11:41
標(biāo)題: [有趣的Python 6] PyGtk中窗口的事件處理
如何在一個(gè)notebook的頁面內(nèi)精確定位

notebook分為標(biāo)簽和頁面兩項(xiàng),如何將他們標(biāo)識(shí)出來,這樣才能完成對(duì)他們的操作




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2