额··游戏测试,经常要get一些道具的ID,但小弟记忆力实在不敢恭维···就边学边写一个物品搜索的工具,方便自己测试···呵呵···
例子分3个文件,gui.py是界面包(好吧,我是用glade画的),eventhandler是事件处理包,main就是执行文件了···主要是调用前2个包,废话不说·
ps:如果弄明白了,gtk的一些组件应该都会比较熟悉的了
先来看一下画面撒:
- #coding=gb2312
-
#-------------------------------------------------------------------------------
-
# Name: ??1
-
# Purpose:
-
#
-
# Author: weigun
-
#
-
# Created: 11-08-2011
-
# Copyright: (c) weigun 2011
-
# Licence: <your licence>
-
#-------------------------------------------------------------------------------
-
#!/usr/bin/env python
-
import gtk
-
import pygtk
-
class Gui():
-
def __init__(self):
-
gtk.rc_parse_string('''style "default-font"\n{\nfont_name = "simsun 9"\n}\nwidget_class "*" style "default-font"\ngtk-font-name = "simsun 9"''')
-
self.d_history = {}
-
self.d_items= {}
-
self.builder = gtk.Builder()
-
self.ui_file = r'itemSearcher.glade'
-
self.gui = self.builder.add_from_file(self.ui_file)
-
#auto entry这个是比较实用的控件喔··记录输入框的输入
-
self.cb_entry = self.builder.get_object('cb_entry')
-
self.entry_completion = gtk.EntryCompletion()
-
self.LS_entry = self.builder.get_object('LS_entry')
-
self.entry_completion.set_model(self.LS_entry)
-
self.entry_completion.set_text_column(0)
-
self.entry_completion.set_inline_completion(1)
-
self.entry = self.cb_entry.child
-
#menuItem
-
self.MI_copy = self.builder.get_object('MI_copy')
-
self.MI_other_gm = self.builder.get_object('MI_other_gm')
-
-
#combobox
-
self.combox = self.builder.get_object('cb_item_type')
-
#self.LS_item_type = self.builder.get_object('LS_item_type')
-
cell = gtk.CellRendererText()
-
self.combox.pack_start(cell, True)
-
self.combox.add_attribute(cell, 'text', 0)
-
#search button
-
self.bt_search = self.builder.get_object('bt_search')
-
#statusbar
-
self.statusbar = self.builder.get_object('statusbar')
-
#treeview
-
self.treeview = self.builder.get_object('treeview')
-
self.LS_content = gtk.ListStore(str,str,str,str) #####
-
self.treeview.set_model(self.LS_content)
-
self.make_header()
-
self.popup_menu= self.builder.get_object('popup_menu')
-
#self.entry.connect('changed',EventHandler.EventHandler.build_completion)
-
-
##############################
-
#self.load_gm()
-
#self.create_sub_menu()
-
-
def reg_signals_handler(self,Event_handler):
-
self.signal_handler = {'on_window_destroy':gtk.main_quit,
-
'on_exit_menu':Event_handler.exit_menu_clicked,
-
'bt_search_clicked':Event_handler.bt_search_clicked,}
-
-
self.builder.connect_signals(self.signal_handler)
-
self.treeview.connect('button-press-event',Event_handler.Rbt_clicked_in_treeview)
-
self.MI_copy.connect('activate',Event_handler.popup_menu_selection)
-
self.builder.get_object('MI_delete').connect('activate',Event_handler.exit_menu_clicked)
-
self.entry.connect('activate',Event_handler.bt_search_clicked)
-
-
def make_header(self):
-
index = 0
-
for head in [u'序号',u'物品ID',u'物品类型',u'物品名称']: #####
-
column = gtk.TreeViewColumn(head, gtk.CellRendererText(), text=index)
-
column.set_sort_column_id(index)
-
self.treeview.append_column(column)
-
index+=1
-
-
def start(self):
-
self.win = self.builder.get_object('window')
-
self.win.show_all()
-
gtk.main()
-
-
def create_sub_menu(self):
-
for key in self.gm_list:
-
menu = gtk.Menu()
-
self.MI_other_gm.set_submenu(menu)
-
imenu = gtk.MenuItem(self.gm_list[key].decode('gbk'))
-
menu.append(imenu)
-
menu.show_all()
-
return False
- #coding=gb2312
-
#-------------------------------------------------------------------------------
-
# Name: 模块1
-
# Purpose:
-
#
-
# Author: admin
-
#
-
# Created: 22/10/2011
-
# Copyright: (c) admin 2011
-
# Licence: <your licence>
-
#-------------------------------------------------------------------------------
-
#!/usr/bin/env python
-
#初始化时,接受GUI包为参数
-
import gtk
-
import pygtk
-
import sys
-
import sqlite3
-
import win32con
-
import win32clipboard as wincb
-
import re
-
class EventHandler():
-
def __init__(self,UI_class):
-
self.Ui = UI_class
-
self.datafile = r"item.datafile"
-
self.conn = sqlite3.connect(self.datafile)
-
-
def bt_search_clicked(self,widget):
-
#print self.entry.get_text()
-
keyword = self.Ui.entry.get_text()
-
item_type = self.Ui.combox.get_active_text()
-
if keyword == '': return False
-
keyword = re.sub('\s+','',keyword)
-
self.add_history(keyword)
-
self.Ui.LS_content.clear()
-
items = self.query_id_by_name(item_type,keyword)
-
index =1
-
for row in items:
-
iter = self.Ui.LS_content.append()
-
self.Ui.LS_content.set(iter,0,index,1,row[0],2,row[1],3,row[2])
-
index+=1
-
-
-
-
def exit_menu_clicked(self,widget):
-
sys.exit()
-
-
def Rbt_clicked_in_treeview(self,widget,event):
-
#print event
-
if event.button == 2 or event.button == 3:
-
#row_path = widget.get_path_at_pos(int(event.x),int(event.y))
-
#path = row_path[0][0]
-
#col = row_path[1]
-
#col_title = col.get_title()
-
#print "col_titile:",col_title.encode('gbk')
-
self.Ui.popup_menu.popup(None,None,None,3,gtk.get_current_event_time())
-
return True
-
-
def popup_menu_selection(self,widget):
-
row_selected = self.Ui.treeview.get_selection()
-
model,iter = row_selected.get_selected()
-
item_id = model.get_value(iter,1)
-
item_name = model.get_value(iter,3)
-
self.copy_to_clipboard(item_id)
-
print "item id:",item_id,"\t",item_name.encode('gbk')
-
-
-
def add_history(self,keyword):
-
model = self.Ui.cb_entry.get_model()
-
if not self.Ui.d_history.has_key(keyword):
-
self.Ui.d_history[keyword] = 1
-
model.set(model.append(),0,keyword)
-
else :
-
self.Ui.d_history[keyword]+=1
-
return False
-
-
-
def build_completion(self,widget):
-
pass
-
#print 'do build_completion'
-
-
-
def query_id_by_name(self,item_type,keyword):
-
if item_type == u'全部' :
-
sql = "select itemid,itemtype,itemname from item where itemname like '%"+self.to_unicode(keyword)+"%' order by priority,itemid"
-
else :
-
sql = "select itemid,itemtype,itemname from item where itemname like '%"+self.to_unicode(keyword)+"%' and itemtype='"+self.to_unicode(item_type)+"' order by priority,itemid"
-
print sql,"\n"
-
cur = self.conn.cursor()
-
cur.execute(sql)
-
return cur.fetchall()
-
-
def copy_to_clipboard(self,item_id):
-
gm_command = "!# add"+str(item_id)+" 20"
-
wincb.OpenClipboard()
-
wincb.EmptyClipboard()
-
wincb.SetClipboardData(win32con.CF_TEXT,gm_command)
-
wincb.CloseClipboard()
-
-
def to_unicode(self,string):
-
encode = 'utf8'
-
return unicode(string,encode)
- #coding=gb2312
-
#-------------------------------------------------------------------------------
-
# Name: ??1
-
# Purpose:
-
#
-
# Author: weigun
-
#
-
# Created: 11-08-2011
-
# Copyright: (c) weigun 2011
-
# Licence: <your licence>
-
#-------------------------------------------------------------------------------
-
#!/usr/bin/env python
-
import Gui
-
import EventHandler
-
-
if __name__ == "__main__":
-
ui = Gui.Gui()
-
EventHandle = EventHandler.EventHandler(ui)
-
ui.reg_signals_handler(EventHandle)
-
ui.start()
阅读(2302) | 评论(0) | 转发(0) |