这是一个用于为 GUI 创建动态 python 代码的 csh 脚本。
它能做什么?
进入
./dynamic_python -label name,age -frame myframe
它将创建 2 个名为 name,age 的标签,为每个标签和一些其他小部件创建 2 个输入框。我知道这不完全是您所需要的,但请考虑以一种可能的方法来解决您的问题。如果您需要更多帮助,请联系我。将感谢其他用户的反馈:)
dynamic_python 的代码:
#! /bin/csh -f
#echo $argv[1]
#shift
#echo $argv[1]
while ("$#" != 0)
if("$argv[1]" == "-label") then
set label = "$argv[2]"
shift
shift
endif
if("$argv[1]" == "-frame") then
set frame = "$argv[2]"
shift
shift
endif
end
set label = `echo $label | sed 's/,/ /g'`
set lcount = `expr "$#label" + 1`
set lc = 1
#
echo "#! /usr/bin/python" > dy.py
echo "from Tkinter import *" >> dy.py
#
echo "def fun_ok():" >> dy.py
echo " print 'OK_Accepted'" >> dy.py
set label = `echo $label | sed 's/,/ /g'`
set lcount = `expr "$#label" + 1`
set lc = 1
echo " list_out = []" >> dy.py
while ($lc != $lcount)
echo " print $label[$lc].get()" >> dy.py
echo " list_out.append('-$label[$lc]')" >> dy.py
echo " list_out.append($label[$lc].get())" >> dy.py
set lc = `expr "$lc" + 1`
end
echo " print list_out" >> dy.py
#
echo "$frame = Tk()" >> dy.py
#
set label = `echo $label | sed 's/,/ /g'`
set lcount = `expr "$#label" + 1`
set lc = 1
while ($lc != $lcount)
echo $lc $label[$lc]
echo "$label[$lc] = StringVar()" >> dy.py
echo "Label($frame,text="_$label[$lc]_").grid(row = $lc,column = 1)" | sed 's/_/"/g' >> dy.py
echo "Entry($frame,textvariable=$label[$lc]).grid(row=$lc,column=2)" >> dy.py
set lc = `expr "$lc" + 1`
end
echo "Button($frame,text='OK',command=fun_ok).grid(row = $lc,column = 1)" >> dy.py
echo "Button($frame,text='EXIT',command=$frame.quit).grid(row = $lc,column = 2)" >> dy.py
#Button(master, text="OK", command=callback)
#
echo "$frame.mainloop()" >> dy.py
#
chmod 755 *
dy.py