總體思想:python
徹底按照本身的想法來寫的,首先寫模板文件,而後打開模板文件,對模板進行字符串格式化處理,最後再將格式化後的字符串保存到新的文件裏面。若有更好的想法,歡迎交流。程序員
將類似度很高的代碼寫模板文件(widget_template.txt):json
# -*- coding: UTF-8 -*- #!/usr/bin/env python #------------------------------------------------------------------------------- # Name: auto # Purpose: # # Author: Yang # # Created: 13/08/2015 # Copyright: (c) Yang 2015 # Licence: <your licence> #------------------------------------------------------------------------------- from PyQt4 import QtGui,QtCore class %(class_name)s(QtGui.QWidget): def __init__(self, parent=None): super(%(class_name)s, self).__init__(parent) self.items = parent.items if parent else {} self.__init_datas() self.__create_items() self.__set_events() ## self.setModal(True) self.setWindowTitle(u"%(class_title)s") ## self.setWindowFlags(QtCore.Qt.Dialog|QtCore.Qt.WindowCloseButtonHint) self.trans = {"window_title":"%(class_trans)s"} self.items["%(class_prefix)s_ui"] = self self.__trans_items() def __init_datas(self): self.data = {} self.data["mc_id"] = None self.data["snaddr"] = "FF" self.data["spaddr"] = "FF" self.data["rcaddr"] = "FFFF" self.data["operation"] = None self.data["mode"] = "save" self.data["argvs"] = [] def __create_items(self): pass def __set_events(self): pass def __set_ui_config(self): """將數據表示到畫面""" pass def __get_ui_config(self): """將畫面數據保存到結構體""" pass def __trans_items(self): if self.parent() is None: return self.parent().trans_items("%(class_prefix)s") def __send_request(self, data, show_msg=0, msg_revc=None): if self.parent() is None: return {} if data["mc_id"] is None: return {} return self.parent().send_request(data, show_msg, msg_revc) def showEvent(self, event): super(%(class_name)s, self).showEvent(event) def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Escape: self.close() super(%(class_name)s, self).keyPressEvent(event) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) dialog = %(class_name)s() dialog.show() sys.exit(app.exec_())
這個是配置文件(config.json),主要是爲了方便模板文件修改以後,不用修改自動生成文件的代碼。app
{ "template_file":"widget_template.txt", "file_name":"SplashSettings.py", "class_name":"SplashSettings", "class_trans":"splash_setting", "class_title":"開機畫面設置", "class_prefix":"sd_ss" }
下面是自動生成文件的代碼(widget_robot.py)工具
# -*- coding: UTF-8 -*- #!/usr/bin/env python #------------------------------------------------------------------------------- # Name: UI自動生成工具 # Purpose: # # Author: Yang # # Created: 13/08/2015 # Copyright: (c) Yang 2015 # Licence: <your licence> #------------------------------------------------------------------------------- import json import codecs def main(): # read config config = {} with codecs.open("config.json","rb","UTF-8") as f: config = json.loads(f.read()) if not config: return # read template file s = "" template = config.get("template_file", "widget_template.txt") with codecs.open(template, "rb", "UTF-8") as f: s = f.read() if not s: return s = s % config # save to file fn = config["file_name"] with codecs.open(fn, "wb", "UTF-8") as f: f.write(s) f.flush() if __name__ == '__main__': try: main() except Exception,ex: print(ex)
總結:ui
其實很簡單的,只要是寫過代碼的人都能寫出來。關鍵看你想不想去寫。spa
第一次寫筆記。祝我在程序員的道路上越走越遠。code