網路上充斥著不能用的範例,大概是版本不同的關係 ... 搞了好久才弄出第一個 hello world ...
hello.py
# coding=utf-8
import gtk
import gtk.glade
class HellowWorld:
def __init__(self):
self.wTree = gtk.glade.XML('hello.glade')
# 抓 glade 檔案裡面的 id="hello"
self.window = self.wTree.get_widget("hello")
# 如果有抓到這個物件
if (self.window):
# 關閉視窗時,執行 gtk.main_quit (其實就是把 GUI 關掉)
self.window.connect("destroy", gtk.main_quit)
# 就是少了這個害我畫面都出不來!
self.window.show()
if __name__ == "__main__":
hwg = HellowWorld()
gtk.main()
import gtk
import gtk.glade
class HellowWorld:
def __init__(self):
self.wTree = gtk.glade.XML('hello.glade')
# 抓 glade 檔案裡面的 id="hello"
self.window = self.wTree.get_widget("hello")
# 如果有抓到這個物件
if (self.window):
# 關閉視窗時,執行 gtk.main_quit (其實就是把 GUI 關掉)
self.window.connect("destroy", gtk.main_quit)
# 就是少了這個害我畫面都出不來!
self.window.show()
if __name__ == "__main__":
hwg = HellowWorld()
gtk.main()
這是hello.glade,可以自己用glade3拉拉看
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.3 on Thu Nov 20 00:42:25 2008 -->
<glade-interface>
<widget class="GtkWindow" id="hello">
<child>
<widget class="GtkLabel" id="lab1">
<property name="visible">True</property>
<property name="label" translatable="yes">hello</property>
</widget>
</child>
</widget>
</glade-interface>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.3 on Thu Nov 20 00:42:25 2008 -->
<glade-interface>
<widget class="GtkWindow" id="hello">
<child>
<widget class="GtkLabel" id="lab1">
<property name="visible">True</property>
<property name="label" translatable="yes">hello</property>
</widget>
</child>
</widget>
</glade-interface>
沒有留言:
張貼留言