Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128289
  • 博文数量: 28
  • 博客积分: 2431
  • 博客等级: 大尉
  • 技术积分: 321
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-29 18:40
文章分类

全部博文(28)

文章存档

2011年(3)

2010年(6)

2009年(7)

2008年(12)

分类: Python/Ruby

2011-10-29 20:26:42

额··游戏测试,经常要get一些道具的ID,但小弟记忆力实在不敢恭维···就边学边写一个物品搜索的工具,方便自己测试···呵呵···

例子分3个文件,gui.py是界面包(好吧,我是用glade画的),eventhandler是事件处理包,main就是执行文件了···主要是调用前2个包,废话不说· 
ps:如果弄明白了,gtk的一些组件应该都会比较熟悉的了

先来看一下画面撒:
  1. #coding=gb2312
  2. #-------------------------------------------------------------------------------
  3. # Name: ??1
  4. # Purpose:
  5. #
  6. # Author: weigun
  7. #
  8. # Created: 11-08-2011
  9. # Copyright: (c) weigun 2011
  10. # Licence: <your licence>
  11. #-------------------------------------------------------------------------------
  12. #!/usr/bin/env python
  13. import gtk
  14. import pygtk
  15. class Gui():
  16.     def __init__(self):
  17.         gtk.rc_parse_string('''style "default-font"\n{\nfont_name = "simsun 9"\n}\nwidget_class "*" style "default-font"\ngtk-font-name = "simsun 9"''')
  18.         self.d_history = {}
  19.         self.d_items= {}
  20.         self.builder = gtk.Builder()
  21.         self.ui_file = r'itemSearcher.glade'
  22.         self.gui = self.builder.add_from_file(self.ui_file)
  23.         #auto entry这个是比较实用的控件喔··记录输入框的输入
  24.         self.cb_entry = self.builder.get_object('cb_entry')
  25.         self.entry_completion = gtk.EntryCompletion()
  26.         self.LS_entry = self.builder.get_object('LS_entry')
  27.         self.entry_completion.set_model(self.LS_entry)
  28.         self.entry_completion.set_text_column(0)
  29.         self.entry_completion.set_inline_completion(1)
  30.         self.entry = self.cb_entry.child
  31.         #menuItem
  32.         self.MI_copy = self.builder.get_object('MI_copy')
  33.         self.MI_other_gm = self.builder.get_object('MI_other_gm')

  34.         #combobox
  35.         self.combox = self.builder.get_object('cb_item_type')
  36.         #self.LS_item_type = self.builder.get_object('LS_item_type')
  37.         cell = gtk.CellRendererText()
  38.         self.combox.pack_start(cell, True)
  39.         self.combox.add_attribute(cell, 'text', 0)
  40.         #search button
  41.         self.bt_search = self.builder.get_object('bt_search')
  42.         #statusbar
  43.         self.statusbar = self.builder.get_object('statusbar')
  44.         #treeview
  45.         self.treeview = self.builder.get_object('treeview')
  46.         self.LS_content = gtk.ListStore(str,str,str,str) #####
  47.         self.treeview.set_model(self.LS_content)
  48.         self.make_header()
  49.         self.popup_menu= self.builder.get_object('popup_menu')
  50.         #self.entry.connect('changed',EventHandler.EventHandler.build_completion)

  51.         ##############################
  52.         #self.load_gm()
  53.         #self.create_sub_menu()

  54.     def reg_signals_handler(self,Event_handler):
  55.         self.signal_handler = {'on_window_destroy':gtk.main_quit,
  56.             'on_exit_menu':Event_handler.exit_menu_clicked,
  57.             'bt_search_clicked':Event_handler.bt_search_clicked,}

  58.         self.builder.connect_signals(self.signal_handler)
  59.         self.treeview.connect('button-press-event',Event_handler.Rbt_clicked_in_treeview)
  60.         self.MI_copy.connect('activate',Event_handler.popup_menu_selection)
  61.         self.builder.get_object('MI_delete').connect('activate',Event_handler.exit_menu_clicked)
  62.         self.entry.connect('activate',Event_handler.bt_search_clicked)

  63.     def make_header(self):
  64.         index = 0
  65.         for head in [u'序号',u'物品ID',u'物品类型',u'物品名称']: #####
  66.             column = gtk.TreeViewColumn(head, gtk.CellRendererText(), text=index)
  67.             column.set_sort_column_id(index)
  68.             self.treeview.append_column(column)
  69.             index+=1

  70.     def start(self):
  71.         self.win = self.builder.get_object('window')
  72.         self.win.show_all()
  73.         gtk.main()


  74.     def create_sub_menu(self):
  75.         for key in self.gm_list:
  76.             menu = gtk.Menu()
  77.             self.MI_other_gm.set_submenu(menu)
  78.             imenu = gtk.MenuItem(self.gm_list[key].decode('gbk'))
  79.             menu.append(imenu)
  80.             menu.show_all()
  81.         return False

  1. #coding=gb2312
  2. #-------------------------------------------------------------------------------
  3. # Name: 模块1
  4. # Purpose:
  5. #
  6. # Author: admin
  7. #
  8. # Created: 22/10/2011
  9. # Copyright: (c) admin 2011
  10. # Licence: <your licence>
  11. #-------------------------------------------------------------------------------
  12. #!/usr/bin/env python
  13. #初始化时,接受GUI包为参数
  14. import gtk
  15. import pygtk
  16. import sys
  17. import sqlite3
  18. import win32con
  19. import win32clipboard as wincb
  20. import re
  21. class EventHandler():
  22.     def __init__(self,UI_class):
  23.         self.Ui = UI_class
  24.         self.datafile = r"item.datafile"
  25.         self.conn = sqlite3.connect(self.datafile)

  26.     def bt_search_clicked(self,widget):
  27.         #print self.entry.get_text()
  28.         keyword = self.Ui.entry.get_text()
  29.         item_type = self.Ui.combox.get_active_text()
  30.         if keyword == '': return False
  31.         keyword = re.sub('\s+','',keyword)
  32.         self.add_history(keyword)
  33.         self.Ui.LS_content.clear()
  34.         items = self.query_id_by_name(item_type,keyword)
  35.         index =1
  36.         for row in items:
  37.             iter = self.Ui.LS_content.append()
  38.             self.Ui.LS_content.set(iter,0,index,1,row[0],2,row[1],3,row[2])
  39.             index+=1



  40.     def exit_menu_clicked(self,widget):
  41.         sys.exit()

  42.     def Rbt_clicked_in_treeview(self,widget,event):
  43.         #print event
  44.         if event.button == 2 or event.button == 3:
  45.             #row_path = widget.get_path_at_pos(int(event.x),int(event.y))
  46.             #path = row_path[0][0]
  47.             #col = row_path[1]
  48.             #col_title = col.get_title()
  49.             #print "col_titile:",col_title.encode('gbk')
  50.             self.Ui.popup_menu.popup(None,None,None,3,gtk.get_current_event_time())
  51.             return True

  52.     def popup_menu_selection(self,widget):
  53.         row_selected = self.Ui.treeview.get_selection()
  54.         model,iter = row_selected.get_selected()
  55.         item_id = model.get_value(iter,1)
  56.         item_name = model.get_value(iter,3)
  57.         self.copy_to_clipboard(item_id)
  58.         print "item id:",item_id,"\t",item_name.encode('gbk')


  59.     def add_history(self,keyword):
  60.         model = self.Ui.cb_entry.get_model()
  61.         if not self.Ui.d_history.has_key(keyword):
  62.             self.Ui.d_history[keyword] = 1
  63.             model.set(model.append(),0,keyword)
  64.         else :
  65.             self.Ui.d_history[keyword]+=1
  66.         return False


  67.     def build_completion(self,widget):
  68.         pass
  69.         #print 'do build_completion'


  70.     def query_id_by_name(self,item_type,keyword):
  71.         if item_type == u'全部' :
  72.             sql = "select itemid,itemtype,itemname from item where itemname like '%"+self.to_unicode(keyword)+"%' order by priority,itemid"
  73.         else :
  74.             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"
  75.         print sql,"\n"
  76.         cur = self.conn.cursor()
  77.         cur.execute(sql)
  78.         return cur.fetchall()

  79.     def copy_to_clipboard(self,item_id):
  80.         gm_command = "!# add"+str(item_id)+" 20"
  81.         wincb.OpenClipboard()
  82.         wincb.EmptyClipboard()
  83.         wincb.SetClipboardData(win32con.CF_TEXT,gm_command)
  84.         wincb.CloseClipboard()

  85.     def to_unicode(self,string):
  86.         encode = 'utf8'
  87.         return unicode(string,encode)

  1. #coding=gb2312
  2. #-------------------------------------------------------------------------------
  3. # Name: ??1
  4. # Purpose:
  5. #
  6. # Author: weigun
  7. #
  8. # Created: 11-08-2011
  9. # Copyright: (c) weigun 2011
  10. # Licence: <your licence>
  11. #-------------------------------------------------------------------------------
  12. #!/usr/bin/env python
  13. import Gui
  14. import EventHandler

  15. if __name__ == "__main__":
  16.     ui = Gui.Gui()
  17.     EventHandle = EventHandler.EventHandler(ui)
  18.     ui.reg_signals_handler(EventHandle)
  19.     ui.start()
阅读(2302) | 评论(0) | 转发(0) |
1

上一篇:Pyscripter总是报错

下一篇:没有了

给主人留下些什么吧!~~