- //[C/C++]代码
- // 调用PyMain模块的PyAppInit, 该函数是程序入口
- boost::python::handle<>* _module = NULL; // Module handle.
- try
- {
- _module = new boost::python::handle<>(
- PyImport_ImportModule("GenerateCode"));
- bool ret = boost::python::call_method<bool>(_module->get(), "GenerateCode");
- //CPaintManagerUI::MessageLoop();
- }
- catch(boost::python::error_already_set const &)
- {
- std::string err = parse_python_exception();
- LOGEx1(err.c_str());
- PyErr_Clear();
- }
- catch (...)
- {
- if (PyErr_Occurred())
- {
- std::string err = parse_python_exception();
- PyErr_Clear();
- }
- }
- //[Python]代码
- # coding=gbk
- #__author__ = 'huqinghua'
- import string, os, commands, time
- import ctypes
- import xml.etree.cElementTree as ET
- codeTemplate ="""# coding=gbk
- __author__ = 'generated by py-ui4win'
- import string, os, time
- from PyUI import *
- from MsgBox import *
- from PyFrameBase import *
- import UICommon
- from CommonUtil import CommonUtils
- from PyWin32Utils import PyWin32Util
- class {CLASS_NAME}(PyFrameBase):
- def __init__(self):
- super({CLASS_NAME}, self).__init__()
- self.clsName = self.__class__.__name__
- self.skinFileName = self.__class__.__name__ + '.xml'
- # 不要改动
- def GetSkinFile(self):
- return self.skinFileName
- # 不要改动
- def GetWindowClassName(self):
- return self.clsName
- # 退出处理
- def OnExit(self, sendor, wParam, lParam):
- #退出程序
- self.ExitApp()
- # 本窗口定时器处理
- # wParam: 定时器id
- def OnCustomTimer(self, wParam, lParam):
- pass
- # 准备显示前的处理
- def OnPrepare(self, sendor, wParam, lParam):
- #声明控件
- {ON_PREPARE}
- # 界面事件处理
- def OnNotify(self, sendor, sType, wParam, lParam):
- # 用户点击事件
- if sType == DUI_MSGTYPE_CLICK:
- {ON_CLICK}
- # 用户选择事件
- if sType == DUI_MSGTYPE_ITEMSELECT:
- {ON_ITEMSELECT}
- """
- class GenerateCode():
- def __init__(self):
- self.code = ''
- def GenerateCode(self, skinXmlPath):
- self.skinXmlPath = skinXmlPath
- if not os.path.isfile(skinXmlPath):
- return -2
- # 分析xml皮肤
- tree = ET.ElementTree(file=skinXmlPath)
- prepare_code = ''
- click_code = ''
- itemselect_code = ''
- for ctltag in ['Control', 'Label', 'Button', 'Option', 'Edit', 'RichEdit','Combo','Text','CheckBox', \\
- 'Progress', 'Animation', 'Container', 'HorizontalLayout', 'VerticalLayout', 'TabLayout', 'List', \\
- 'webBrowser']:
- for elem in tree.iter(tag=ctltag):
- if elem.attrib.has_key('name'):
- if elem.attrib['name'] not in ['minbtn', 'maxbtn', 'restorebtn', 'closebtn']:
- #print elem.tag, elem.attrib
- # OnPrepare
- prepare_code += ' self.%s = self.PyFind%s("%s")'%(elem.attrib['name'], ctltag, elem.attrib['name']) + os.linesep
- # DUI_MSGTYPE_CLICK
- if ctltag in ['Button', 'Option', 'CheckBox']:
- click_code += ' elif sendor == "%s": #点击了%s'%(elem.attrib['name'], elem.attrib['name']) + os.linesep
- click_code += ' pass' + os.linesep
- # DUI_MSGTYPE_ITEMSELECT
- if ctltag in ['Combo', 'List']:
- itemselect_code += ' elif sendor == "%s":'%elem.attrib['name'] + os.linesep
- itemselect_code += ' pass' + os.linesep
- click_code = click_code.replace('elif', 'if', 1)
- itemselect_code = itemselect_code.replace('elif', 'if', 1)
- # 组合代码
- self.saveFile = skinXmlPath.replace('.xml','.py')
- self.codeTemp = codeTemplate.replace('\\n', '\\r\\n')
- self.code = self.codeTemp.format(\\
- CLASS_NAME = os.path.basename(skinXmlPath).split('.')[0],\\
- ON_PREPARE = prepare_code,\\
- ON_CLICK = click_code if click_code != '' else ' pass',\\
- ON_ITEMSELECT = itemselect_code if itemselect_code != '' else ' pass'\\
- )
- #写py文件
- outfile = open(self.saveFile, 'wb')
- outfile.write(self.code)
- outfile.close()
- #打开文件
- shell32 = ctypes.windll.LoadLibrary("shell32.dll");
- shell32.ShellExecuteA(None,'open', 'notepad',self.saveFile,'',1);
- return 0
- def GeneratePythonCode( skinXmlPath):
- return GenerateCode().GenerateCode(skinXmlPath)
- if __name__ == '__main__':
- GeneratePythonCode(r'f:\\pyui4win\\pyui4win\\Demo3\\skin\\MainFrame.xml')
- //该片段来自于http://www.codesnippet.cn/detail/040520149445.html
来源: http://www.codesnippet.cn/detail/040520149445.html