#=============================================================================== #[窗口/对话框] # 绑定事件处理函数 self.win.connect("delete-event",gtk.main_quit) # "about"对话框 def about(self, widget): about = gtk.AboutDialog() about.set_name("xxx软件") about.set_version("V1.0") about.set_authors(["xxx有限公司"]) about.set_copyright('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 # 显示在父窗口中央 self.win.set_transient_for(self.parent.win) # 弹出提示对话框 msg_dlg = gtk.MessageDialog (self.win,/ 0,/ gtk.MESSAGE_ERROR,/ gtk.BUTTONS_CLOSE,/ "请输入有效的信息!" ) msg_dlg.run() # 按下任何按钮后返回 msg_dlg.destroy() # 显示或隐藏 self.win.show() self.win.hide() # 窗口通信方式 class OtherW(gtk.Window) def get_data_user_entered(self): return 42 class W(gtk.Window): def user_clicked_button(self): self.ow = OtherW() self.ow.button.connect("clicked", self._get_data) def _get_data(self, *args): data = self.ow.get_data_user_entered() w = W() w.show_all() gtk.main() #=============================================================================== #[TextView] # 设置文本 buf = gtk.TextBuffer() buf.set_text("string") self.tv.set_buffer(buf) # 获取文本 #=============================================================================== #[TreeView] # 获取/修改当前选中行的数据 (treestore, iter) = self.treeview.get_selection().get_selected() item = treestore.get_value(iter, 0) # 获取第0列数据 treestore.set_value(iter, n, item) # 修改第n列数据 # 移除所有已经存在的列头 for col in self.treeview.get_columns(): self.treeview.remove_column(col) # 插入数据 n=0 for head in ['col_1','col_2','col_3']: 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.list_store.append(['text_1','text_2','text_3']) self.treeview.show() # 双击行事件处理 # 绑定信号"row_activated"与该函数 def double_click_row(self, treeview, path, view_column): self.tv_model = treeview.get_model() self.tv_iter = self.tv_model.get_iter(path) # 获取该行每一列的值 for n in range(0, self.tv_model.get_n_columns()): item = self.tv_model.get_value(self.tv_iter, n) #=============================================================================== #[进度条(progressbar)] # 显示进度条 n = 1 while n < 100: self.progressbar.set_fraction(float(n)/100) n += 1 # 立即刷新进度条 while gtk.events_pending(): gtk.main_iteration_do(False) time.sleep(0.05) self.progressbar.set_fraction(0.0) # 设置背景文本 self.progressbar.set_text("text") #=============================================================================== #[菜单] # disable菜单 self.menu.set_sensitive(False) # 触发菜单事件 self.menu.activate() #=============================================================================== #[其他] # 设置字体大小 context = self.progressbar.get_pango_context() font = context.get_font_description() font.set_size(int(font.get_size() * 1.3)) # 设置为原来的1.3倍大小 font.set_weight(700) # 设置为Bold(对应700) font.set_family("Monospace") # 设置字体 self.progressbar.modify_font(font) # 检测按键 # 绑定按键处理函数 self.win.connect("key-press-event",self.on_key_press) # 按键处理 def on_key_press(self, window, event): keyname = gtk.gdk.keyval_name(event.keyval) if keyname == "space": print keyname return # 设置标签(label)文本 self.label.set_text("string") #=============================================================================== #[输入框(entry)] # 获取/设置输入框的文本 text = self.entry.get_text() self.entry.set_text("string") # 设置为不可见输入模式 self.passwd.set_visibility(False) # 设置背景色 self.entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse("#DCDCDC")) # 设置输入焦点 self.entry.grab_focus()