pygtk程序模板

运行方法:

1.将代码保存为两个文件test.py、test.glade。

2.运行python test.py

 

test.py :

#!/usr/bin/env python # -*- coding: utf-8 -*- # 在代码中支持中文 import sys import gobject import MySQLdb import time import gtk import gtk.glade try: import pygtk pygtk.require("2.0") except: sys.exit(1) class win_main: def __init__(self): # get all widgets self.wTree = gtk.glade.XML("test.glade", "win_main") self.win = self.wTree.get_widget("win_main") self.treeview = self.wTree.get_widget("treeview1") # 绑定信号处理方法 dic = { "on_menu_add_activate" : self.on_add,/ "on_quit_activate" : gtk.main_quit,/ "on_about_activate" : self.on_about } self.wTree.signal_autoconnect(dic) # glade bug: 不能保存主窗口的某些信号绑定,那就手动吧! self.win.connect("delete-event",gtk.main_quit) # 窗口右上角的"x"按钮事件 # 初始化TreeView n = 0 for head in ['A','B','C']: column = gtk.TreeViewColumn(head, gtk.CellRendererText(), text=n) column.set_resizable(True) column.set_sort_column_id(n) # 使列头可点击排序 self.treeview.append_column(column) n=n+1 self.list_store = gtk.ListStore(str,str,str) self.treeview.set_model(self.list_store) self.treeview.show() return # 加入数据到TreeView def add(self, data): self.list_store.append(data) self.treeview.show() return # 处理菜单事件 def on_add(self, widget): # 创建“添加”对话框 dlg = dlg_add(self) return # 处理菜单事件 def on_about(self, widget): about = gtk.AboutDialog() about.set_name("xxx软件") about.set_version("V1.0") #about.set_authors(["xxx有限公司"]) about.set_copyright('xxx有限公司, 2009-2010') about.set_website('http://www.xxx.com.cn') #icon = gtk.gdk.pixbuf_new_from_file("xxx.jpg") #about.set_logo(icon) #about.set_icon(icon) about.run() # 按下任何按钮后该函数返回 about.destroy() return class dlg_add: # parent = 父窗口对应类 def __init__(self, parent): self.parent = parent # get all widgets self.wTree = gtk.glade.XML("test.glade", "dlg_add") self.win = self.wTree.get_widget("dlg_add") self.entry_a = self.wTree.get_widget("entry_a") self.entry_b = self.wTree.get_widget("entry_b") self.entry_c = self.wTree.get_widget("entry_c") # 绑定信号处理方法 dic = { "on_bt_add_clicked" : self.on_add,/ "on_bt_quit_clicked" : self.on_quit } self.wTree.signal_autoconnect(dic) # 显示在父窗口中央 self.win.set_transient_for(self.parent.win) self.win.show() return # 处理按钮事件 def on_add(self, widget): # 获取用户输入 text_a = self.entry_a.get_text() text_b = self.entry_b.get_text() text_c = self.entry_c.get_text() # 调用父窗口的类的方法 self.parent.add([text_a,text_b,text_c]) self.win.destroy() return # 处理按钮事件 def on_quit(self, button): self.win.destroy() return def main(): app = win_main() gtk.main() if __name__ == "__main__": main()

 

 

 

 

test.glade :

<?xml version="1.0"?> <glade-interface> <!-- interface-requires gtk+ 2.16 --> <!-- interface-naming-policy toplevel-contextual --> <widget class="GtkWindow" id="win_main"> <property name="visible">True</property> <property name="title" translatable="yes">main</property> <property name="window_position">center</property> <property name="default_width">850</property> <property name="default_height">600</property> <child> <widget class="GtkVBox" id="vbox1"> <property name="visible">True</property> <property name="orientation">vertical</property> <child> <widget class="GtkMenuBar" id="menubar1"> <property name="visible">True</property> <child> <widget class="GtkMenuItem" id="operation"> <property name="visible">True</property> <property name="label" translatable="yes">菜单 (_M)</property> <property name="use_underline">True</property> <signal name="activate" handler="on_operation_activate"/> <child> <widget class="GtkMenu" id="menu1"> <property name="visible">True</property> <child> <widget class="GtkMenuItem" id="menu_add"> <property name="visible">True</property> <property name="label" translatable="yes">添加</property> <property name="use_underline">True</property> <signal name="activate" handler="on_menu_add_activate"/> <accelerator key="a" signal="activate" modifiers="GDK_CONTROL_MASK"/> </widget> </child> <child> <widget class="GtkSeparatorMenuItem" id="menuitem"> <property name="visible">True</property> </widget> </child> <child> <widget class="GtkMenuItem" id="quit"> <property name="visible">True</property> <property name="label" translatable="yes">退出</property> <property name="use_underline">True</property> <signal name="activate" handler="on_quit_activate"/> <accelerator key="q" signal="activate" modifiers="GDK_CONTROL_MASK"/> </widget> </child> </widget> </child> </widget> </child> <child> <widget class="GtkMenuItem" id="help"> <property name="visible">True</property> <property name="label" translatable="yes">帮助 (_H)</property> <property name="use_underline">True</property> <child> <widget class="GtkMenu" id="menu2"> <property name="visible">True</property> <child> <widget class="GtkMenuItem" id="about"> <property name="visible">True</property> <property name="label" translatable="yes">关于</property> <property name="use_underline">True</property> <signal name="activate" handler="on_about_activate"/> </widget> </child> </widget> </child> </widget> </child> </widget> <packing> <property name="expand">False</property> <property name="position">0</property> </packing> </child> <child> <widget class="GtkScrolledWindow" id="scrolledwindow1"> <property name="visible">True</property> <property name="can_focus">True</property> <property name="shadow_type">in</property> <child> <widget class="GtkTreeView" id="treeview1"> <property name="visible">True</property> <property name="can_focus">True</property> <signal name="row_activated" handler="row_activated"/> </widget> </child> </widget> <packing> <property name="position">1</property> </packing> </child> </widget> </child> </widget> <widget class="GtkDialog" id="dlg_add"> <property name="border_width">5</property> <property name="title" translatable="yes">add</property> <property name="resizable">False</property> <property name="modal">True</property> <property name="type_hint">dialog</property> <property name="has_separator">False</property> <child internal-child="vbox"> <widget class="GtkVBox" id="dialog-vbox5"> <property name="visible">True</property> <property name="orientation">vertical</property> <property name="spacing">2</property> <child> <widget class="GtkTable" id="table1"> <property name="visible">True</property> <property name="n_rows">3</property> <property name="n_columns">2</property> <child> <widget class="GtkEntry" id="entry_a"> <property name="visible">True</property> <property name="can_focus">True</property> <property name="invisible_char">●</property> </widget> <packing> <property name="left_attach">1</property> <property name="right_attach">2</property> </packing> </child> <child> <widget class="GtkEntry" id="entry_b"> <property name="visible">True</property> <property name="can_focus">True</property> <property name="invisible_char">●</property> </widget> <packing> <property name="left_attach">1</property> <property name="right_attach">2</property> <property name="top_attach">1</property> <property name="bottom_attach">2</property> </packing> </child> <child> <widget class="GtkEntry" id="entry_c"> <property name="visible">True</property> <property name="can_focus">True</property> <property name="invisible_char">●</property> </widget> <packing> <property name="left_attach">1</property> <property name="right_attach">2</property> <property name="top_attach">2</property> <property name="bottom_attach">3</property> </packing> </child> <child> <widget class="GtkLabel" id="label1"> <property name="visible">True</property> <property name="label" translatable="yes">A:</property> </widget> </child> <child> <widget class="GtkLabel" id="label2"> <property name="visible">True</property> <property name="label" translatable="yes">B:</property> </widget> <packing> <property name="top_attach">1</property> <property name="bottom_attach">2</property> </packing> </child> <child> <widget class="GtkLabel" id="label3"> <property name="visible">True</property> <property name="label" translatable="yes">C:</property> </widget> <packing> <property name="top_attach">2</property> <property name="bottom_attach">3</property> </packing> </child> </widget> <packing> <property name="position">1</property> </packing> </child> <child internal-child="action_area"> <widget class="GtkHButtonBox" id="dialog-action_area5"> <property name="visible">True</property> <property name="layout_style">center</property> <child> <widget class="GtkButton" id="bt_add"> <property name="label" translatable="yes">添加</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <signal name="clicked" handler="on_bt_add_clicked"/> </widget> <packing> <property name="expand">False</property> <property name="fill">False</property> <property name="position">0</property> </packing> </child> <child> <widget class="GtkButton" id="bt_quit"> <property name="label" translatable="yes">退出</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <signal name="clicked" handler="on_bt_quit_clicked"/> </widget> <packing> <property name="expand">False</property> <property name="fill">False</property> <property name="position">1</property> </packing> </child> </widget> <packing> <property name="expand">False</property> <property name="pack_type">end</property> <property name="position">0</property> </packing> </child> </widget> </child> </widget> </glade-interface>

 

你可能感兴趣的:(Class,import,BT,menu,Signal,website)