在新版本的Glade软件中,比如我使用的Glade 3.8.3版本,在保存布局文件时有两种格式供选择,一种是GtkBuilder,还有一种是Libglade,并且默认格式是GtkBuilder.
两种格式在python中的导入方式不一样:
Libglade格式导入代码:
#!/usr/bin/python2
# coding: utf-8
import gtk
import gtk.glade #注意引入这个包
class clyMPWindow():
"""clyMP的主界面"""
def __init__(self):
self.gladeFile = r'/root/Works/python/clyMP.glade'
# self.builder = gtk.Builder()
# self.builder.add_from_file(self.gladeFile)
self.views = gtk.glade.XML(self.gladeFile)
self.mainWindow = self.views.get_widget('gladeMainWindow')
# self.mainWindow = self.builder.get_object('gladeMainWindow')
if self.mainWindow:
self.mainWindow.connect('destroy', gtk.main_quit)
self.mainWindow.show_all()
if __name__ == '__main__':
ui = clyMPWindow()
gtk.main()
GtkBuilder格式文件导入代码:
#!/usr/bin/python2
# coding: utf-8
import gtk
class clyMPWindow():
"""clyMP的主界面"""
def __init__(self):
self.gladeFile = r'/root/Works/python/clyMP.builder'
self.builder = gtk.Builder()
self.builder.add_from_file(self.gladeFile)
# self.views = gtk.glade.XML(self.gladeFile)
# self.mainWindow = self.views.get_widget('gladeMainWindow')
self.mainWindow = self.builder.get_object('gladeMainWindow')
if self.mainWindow:
self.mainWindow.connect('destroy', gtk.main_quit)
self.mainWindow.show_all()
if __name__ == '__main__':
ui = clyMPWindow()
gtk.main()
GtkBuilder格式比Libglade格式支持更多的功能,比如TreeView所使用的数据容器liststore,应是一种更新的存储格式。