Python自動生成代碼 - 通過tkinter圖形化操作并生成代碼框架
- 背景
- 腳本代碼
- Demo_CodeGenerator.py
- display.py
- FileHandler.py:
- 腳本運行結果:
- 腳本代碼目錄
背景
在寫代碼過程中,如果有 頻繁重復性的編碼操作 ,或者可以 Reuse 的各類代碼,可以通過Python寫一個腳本, 自動生成這類代碼 ,就不用每次手寫、或者copy了。
比如新建固定的代碼框架、添加一些既定的軟件邏輯,通訊協議、消息模板等等,再編寫一套代碼時,或者一個Function時,每次使通過腳本一鍵生成代碼,就不需要每次都寫一遍了,同時可以把相關軟件邏輯放進去,也能避免出錯。
腳本代碼
Demo_CodeGenerator.py
具體詳細代碼去掉了,大家想生成什么樣的代碼就在mycode中append相應的行,然后點開Display輸入相關參數,就可以自動生成code文件。
#conding=utf-8
from
FileHandler
import
WritetoFile
import
sys
PwdPath
=
sys
.
argv
[
0
]
class
BuildCode_Dev
:
'Auto Generate code of Device control'
def
__init__
(
self
,
KeyWord
=
'TestDemoCommand'
)
:
self
.
CmdKeyWord
=
KeyWord
def
Generate
(
self
)
:
fileName
=
'Code_Dev.txt'
mycode
=
[
]
#想生成什么樣的代碼就在mycode中append相應的行,
mycode
.
append
(
'\n---------------------- Demo code Below: ---------------------- '
)
mycode
.
append
(
'\n---------------------- Demo code Below: ---------------------- '
)
mycode
.
append
(
'\n---------------------- Demo code Below: ---------------------- '
)
mycode
.
append
(
'\n***** DemoCode_Get_DataLength() ***** '
)
mycode
.
append
(
'\n***** DemoCode_Set_DataLength() ***** '
)
mycode
.
append
(
'Switch ('
+
self
.
CmdKeyWord
+
'):'
)
mycode
.
append
(
' case('
+
self
.
CmdKeyWord
+
'):'
)
mycode
.
append
(
' break;'
)
WritetoFile
(
fileName
,
mycode
)
print
(
'Code:'
+
self
.
CmdKeyWord
+
' Generator OK!'
)
return
(
mycode
)
if
__name__
==
'__main__'
:
if
(
sys
.
argv
[
1
:
]
==
[
]
)
:
print
(
'Not input parameter , Use Test Data'
)
CmdKeyWord
=
'TestDemoCommand'
else
:
CmdKeyWord
=
sys
.
argv
[
1
]
#code = BuildCode_Dev(CmdKeyWord)
#code.Generate()
#print(PwdPath)
#print(CmdKeyWord)
display.py
主要通過tkinter圖形化顯示,根據輸入參數,點擊按鈕 生成相應的代碼。
# -*- coding: utf-8 -*-
from
tkinter
import
*
from
GatewayControl_CodeGenerator
import
*
from
DeviceControl_CodeGenerator
import
*
def
GWMsg
(
)
:
txt
.
delete
(
1.0
,
END
)
Input
=
str
(
inputData
.
get
(
1.0
,
END
)
)
Cmd_Gw
=
BuildCode_GW
(
Input
)
CodeRst
=
Cmd_Gw
.
Generate
(
)
s
=
'Code_GW.txt Generate Success:\n\n'
txt
.
insert
(
END
,
s
)
txt
.
insert
(
END
,
CodeRst
)
#inputData.delete(0.0, END)
def
DevMsg
(
)
:
txt
.
delete
(
1.0
,
END
)
Input
=
str
(
inputData
.
get
(
1.0
,
END
)
)
Cmd_Dev
=
BuildCode_Dev
(
Input
)
CodeRst
=
Cmd_Dev
.
Generate
(
)
s
=
'Code_Dev.txt Generate Success:\n\n'
txt
.
insert
(
END
,
s
)
txt
.
insert
(
END
,
CodeRst
)
#inputData.delete(0.0, END)
def
clearContent
(
)
:
inputData
.
delete
(
1.0
,
END
)
txt
.
delete
(
1.0
,
END
)
root
=
Tk
(
)
root
.
geometry
(
'1000x600'
)
root
.
title
(
' Code Generator'
)
root
.
config
(
bg
=
'#f0ffff'
)
#Lable
intro
=
Label
(
root
,
text
=
'請在左側輸入消息/命令名字, 然后選擇相應按鈕生成代碼'
,
\
bg
=
'#d3fbfb'
,
\
fg
=
'red'
,
\
font
=
(
'華文新魏'
,
11
)
,
\
width
=
20
,
\
height
=
2
,
\
relief
=
RIDGE
)
intro
.
place
(
relx
=
0.1
,
rely
=
0.1
,
relwidth
=
0.8
,
relheight
=
0.1
)
#Input
inputData
=
Text
(
root
,
font
=
(
''
,
14
)
)
inputData
.
place
(
relx
=
0.1
,
rely
=
0.2
,
relwidth
=
0.3
,
relheight
=
0.6
)
#Output
txt
=
Text
(
root
,
font
=
(
''
,
9
)
)
txt
.
place
(
relx
=
0.6
,
rely
=
0.2
,
relwidth
=
0.3
,
relheight
=
0.6
)
#Button
bt_json2bin
=
Button
(
root
,
text
=
'**Demo Control'
,
command
=
GWMsg
,
fg
=
'blue'
)
bt_json2bin
.
place
(
relx
=
0.4
,
rely
=
0.25
,
relwidth
=
0.2
,
relheight
=
0.1
)
bt_bin2json
=
Button
(
root
,
text
=
'**Demo Control'
,
command
=
DevMsg
,
fg
=
'blue'
)
bt_bin2json
.
place
(
relx
=
0.4
,
rely
=
0.45
,
relwidth
=
0.2
,
relheight
=
0.1
)
bt_clear
=
Button
(
root
,
text
=
'Clear'
,
command
=
clearContent
,
fg
=
'blue'
)
bt_clear
.
place
(
relx
=
0.4
,
rely
=
0.65
,
relwidth
=
0.2
,
relheight
=
0.1
)
intro
=
Label
(
root
,
text
=
'產生的完整代碼在 Code_GW.txt或Code_Dev.txt中(當前目錄 ), 問題聯系人:Howard'
,
\
bg
=
'#d3fbfb'
,
\
fg
=
'red'
,
\
font
=
(
'華文新魏'
,
11
)
,
\
width
=
20
,
\
height
=
2
,
\
relief
=
RIDGE
)
intro
.
place
(
relx
=
0.1
,
rely
=
0.8
,
relwidth
=
0.8
,
relheight
=
0.1
)
root
.
mainloop
(
)
FileHandler.py:
主要是把代碼寫入文件保存
def
WritetoFile
(
FileName
,
Data
)
:
with
open
(
FileName
,
'w'
)
as
record
:
strr
=
"\n"
content
=
strr
.
join
(
Data
)
record
.
write
(
content
+
'\n'
)
腳本運行結果:
運行display.py, 選擇相應按鈕后,會調用相應生成代碼py文件。
最后可以把這個打包成一個exe,方便使用。
腳本代碼目錄
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
