GUI2Python库 - 用aardio画Tkinter界面并生成Python代码
TKinter是Python的原生GUI库,用来创建一些简单的GUI界面还是很方便的。但是,缺点是没有提供一套图形化的设计界面,这样创建界面的时候就非常不直观。
思路是搞一个类似QtDesigner类似的工具,但是又不想搞得太复杂。
aardio的图形化设计非常便捷,这样在aardio里画好界面自动生成tkinter的python代码就是再好不过的事情了。
已开源至Github:
https://github.com/jerryxjr1220/aardioGUI2Python
视频教程:
https://www.bilibili.com/video/BV1wN4y1L7Wk/?vd_source=0400f0a70c0250d73895963c7c937f75
20220807更新:
1. 优化代码增强可读性,功能不变。
20220728更新:
1. 针对标签、文本框等有字符布局的控件增加左对齐、居中、右对齐布局
2. 多行文本框、画板、Listbox增加水平和垂直滚动条
3. 新增translateName函数,解析窗体设计器代码,返回的Z序与对应控件名称,可用来替换python中的控件名
20220727更新:
新增字体属性设置,可按照aardio中的字体自动设置到Tkinter中
修改transfer2assembly名称 为 transfer2place,更好地对应place方法
20220725更新:
所有可禁用控件,增加禁用属性
文本框增加密码属性
新增pack布局和grid布局
20220724更新:
目前可以实现的功能:
基本控件都已经完善
可以把界面直接转换为主界面
也可以支持把界面转换为组件式的类,实例化以后可以重复利用,也可以嵌入到其他Frame、Notebook、LabelFrame等容器中,当然也可以是主窗口Tk容器。
import GUI2Py; //解析aardio界面生成Python Tkinter import color import string import math namespace GUI2Py class GUI2Tk { ctor( winform ){ this = winform; this.pycode = ""; }; write = function(code, space=0, end_='\r\n'){ if space>0 { for i=1;space { this.pycode += ' '; } } this.pycode += code++end_; }; //转换为主界面 transfer2root = function(){ var window_width = this.width; var window_height = this.height; var window_text = this.text; this.pycode = /*** import tkinter as tk import tkinter.ttk as ttk import tkinter.font as tkFont root = tk.Tk() ### 界面设计部分 ### ***/; owner.write('root.geometry("'++ window_width ++ 'x' ++ window_height ++'")'); owner.write('root.title("'++ window_text ++'")'); var ctrlNameList = { 'button' = 0; 'static' = 0; 'edit' = 0; 'radio' = 0; 'check' = 0; 'pic' = 0; 'listbox' = 0; 'combobox' = 0; 'progress' = 0; 'trackbar' = 0; 'listview' = 0; 'treeview' = 0; 'canvas' = 0; 'groupbox' = 0; 'frame' = 0; 'tab' = 0; }; for ( hwnd,ctrl in this.eachControlEx() ){ owner.write('# Z='++ctrl.z); winctrol = this.getCtrl(hwnd); var x = winctrol.left; var y = winctrol.top; var width = winctrol.width; var height = winctrol.height; var text = winctrol.text; var hide = ctrl.hide; var disabled = ctrl.disabled; var readonly = ctrl.readonly; var bgcolor = ctrl.bgcolor; var frcolor = ctrl.color; var font = ctrl.font; var align = ctrl.align; var justify; if !align { justify = ', justify="left"'; } elseif align == "center" { justify = ', justify="center"'; } else { justify = ', justify="right"'; }; //按钮 if winctrol.cls == "button" { var id = ctrlNameList.button + 1; ctrlNameList.button = id; owner.write('button'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); owner.write('button'++id++ ' = ttk.Button(button'++id++'_frame, text="'++ text ++ '")'); owner.write('button'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('button'++id++ '.place_forget()'); }; if disabled { owner.write('button'++id++ '.configure(state="disabled")'); }; owner.write('button'++id++'_frame.place(x='++ x ++', y='++ y ++')'); }; //标签 if winctrol.cls == "static" { var id = ctrlNameList.static + 1; ctrlNameList.static = id; owner.write('label'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); owner.write('label'++id++ ' = ttk.Label(label'++id++'_frame, text="'++ text ++'"'++ justify ++')'); owner.write('label'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('label'++id++ '.place_forget()'); }; if disabled { owner.write('label'++id++ '.configure(state="disabled")'); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('label'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')'); owner.write('label'++id++'.configure(font=label'++id++'_font)'); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('label'++id++ '.config(background="'++bgcolorStr++'")'); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('label'++id++ '.config(foreground="'++frcolorStr++'")'); }; owner.write('label'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //文本框 if winctrol.cls == "edit" { var id = ctrlNameList.edit + 1; ctrlNameList.edit = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; owner.write('editVar' ++ id ++ " = tk.StringVar(value='"++ winctrol.text ++"')"); owner.write('edit'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); if winctrol.multiline == 1 { owner.write('edit'++id++ ' = tk.Text(edit'++id++'_frame)'); owner.write('edit'++id++ '.insert("end", "'++ winctrol.text ++ '")'); } else { owner.write('edit'++id++ ' = ttk.Entry(edit'++id++'_frame, textvariable=editVar'++id++justify++')'); } owner.write('edit'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if ctrl.password { owner.write('edit'++id++ '.configure(show="*")'); }; if(readonly or disabled) { owner.write('edit'++id++ '.configure(state="disabled")'); }; if hide { owner.write('edit'++id++ '.place_forget()'); }; if vscroll { owner.write('edit'++id++'_vscroll = tk.Scrollbar(edit'++id++', orient="vertical", command=edit'++id++'.yview)'); owner.write('edit'++id++'.configure(yscrollcommand=edit'++id++'_vscroll.set)'); owner.write('edit'++id++'_vscroll.pack(side="right", fill="y")'); }; if hscroll { owner.write('edit'++id++'_hscroll = tk.Scrollbar(edit'++id++', orient="horizontal", command=edit'++id++'.xview)'); owner.write('edit'++id++'.configure(xscrollcommand=edit'++id++'_hscroll.set)'); owner.write('edit'++id++'_hscroll.pack(side="bottom", fill="x")'); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('edit'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')'); owner.write('edit'++id++'.configure(font=edit'++id++'_font)'); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('edit'++id++ '.config(background="'++bgcolorStr++'")'); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('edit'++id++ '.config(foreground="'++frcolorStr++'")'); }; owner.write('edit'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //单选 if winctrol.cls == "radiobutton" { var id = ctrlNameList.radio + 1; ctrlNameList.radio = id; owner.write('radio'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); owner.write('radio'++id++ ' = ttk.Radiobutton(radio'++id++'_frame, text="'++ text ++'"' ++ justify ++ ')'); owner.write('radio'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('radio'++id++ '.place_forget()'); }; if disabled { owner.write('radio'++id++ '.configure(state="disabled")'); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('radio'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')'); owner.write('radio'++id++'.configure(font=radio'++id++'_font)'); }; owner.write('radio'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //复选 if winctrol.cls == "checkbox" { var id = ctrlNameList.check + 1; ctrlNameList.check = id; owner.write('check'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); owner.write('check'++id++ ' = ttk.Checkbutton(check'++id++'_frame, text="'++ text ++'"'++justify++')'); owner.write('check'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('check'++id++ '.place_forget()'); }; if disabled { owner.write('check'++id++ '.configure(state="disabled")'); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('check'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')'); owner.write('check'++id++'.configure(font=check'++id++'_font)'); }; owner.write('check'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //图片框 if winctrol.cls == "picturebox" { var id = ctrlNameList.pic + 1; ctrlNameList.pic = id; owner.write('pic'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); var picpath = winctrol.background; if picpath { owner.write('img' ++ id ++ " = tk.PhotoImage(file=r'"++ picpath.path ++"')"); owner.write('pic' ++ id ++ ' = ttk.Label(pic'++id++'_frame, image=img'++ id ++justify++')'); } else { owner.write('pic'++id++ ' = ttk.Label(pic'++id++'_frame'++justify++')'); } owner.write('pic'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('pic'++id++ '.place_forget()'); }; if disabled { owner.write('pic'++id++ '.configure(state="disabled")'); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('pic'++id++ '.config(background="'++bgcolorStr++'")'); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('pic'++id++ '.config(foreground="'++frcolorStr++'")'); }; owner.write('pic'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //画板 if winctrol.cls == "plus" { var id = ctrlNameList.canvas + 1; ctrlNameList.canvas = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; var bgcolor = winctrol.backgroundColor; owner.write('canvas'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); owner.write('canvas'++id++ ' = tk.Canvas(canvas'++id++'_frame)'); owner.write('canvas'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('canvas'++id++ '.place_forget()'); }; if disabled { owner.write('canvas'++id++ '.configure(state="disabled")'); }; if vscroll { owner.write('canvas'++id++'_vscroll = tk.Scrollbar(canvas'++id++', orient="vertical", command=canvas'++id++'.yview)'); owner.write('canvas'++id++'.configure(yscrollcommand=canvas'++id++'_vscroll.set)'); owner.write('canvas'++id++'_vscroll.pack(side="right", fill="y")'); }; if hscroll { owner.write('canvas'++id++'_hscroll = tk.Scrollbar(canvas'++id++', orient="horizontal", command=canvas'++id++'.xview)'); owner.write('canvas'++id++'.configure(xscrollcommand=canvas'++id++'_hscroll.set)'); owner.write('canvas'++id++'_hscroll.pack(side="bottom", fill="x")'); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); bStr = ..string.slice(bgcolorStr,2,3); gStr = ..string.slice(bgcolorStr,4,5); rStr = ..string.slice(bgcolorStr,6,7); owner.write('canvas'++id++ '.config(background="#'++rStr++gStr++bStr++'")'); }; owner.write('canvas'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //ListBox if winctrol.cls == "listbox" { var id = ctrlNameList.listbox + 1; ctrlNameList.listbox = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; owner.write('list'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); var items = winctrol.items; if #items>0 { owner.write('items'++id++' = tk.Variable(value=[', 0, ""); for i,item in items { owner.write('"' ++ item ++'", ', 0, ""); } owner.write('])'); owner.write('list'++id++ ' = tk.Listbox(list'++id++'_frame, listvariable=items'++ id ++ justify ++')'); } else { owner.write('list'++id++ ' = tk.Listbox(list'++id++'_frame'++justify++')'); } owner.write('list'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('list'++id++ '.place_forget()'); }; if disabled { owner.write('list'++id++ '.configure(state="disabled")'); }; if vscroll { owner.write('list'++id++'_vscroll = tk.Scrollbar(list'++id++', orient="vertical", command=list'++id++'.yview)'); owner.write('list'++id++'.configure(yscrollcommand=list'++id++'_vscroll.set)'); owner.write('list'++id++'_vscroll.pack(side="right", fill="y")'); }; if hscroll { owner.write('list'++id++'_hscroll = tk.Scrollbar(list'++id++', orient="horizontal", command=list'++id++'.xview)'); owner.write('list'++id++'.configure(xscrollcommand=list'++id++'_hscroll.set)'); owner.write('list'++id++'_hscroll.pack(side="bottom", fill="x")'); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('list'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')'); owner.write('list'++id++'.configure(font=list'++id++'_font)'); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('list'++id++ '.config(background="'++bgcolorStr++'")'); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('list'++id++ '.config(foreground="'++frcolorStr++'")'); }; owner.write('list'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //下拉框 if winctrol.cls == "combobox" { var id = ctrlNameList.combobox + 1; ctrlNameList.combobox = id; owner.write('combobox'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); var items = winctrol.items; if #items>0 { owner.write('combobox'++id++ '= ttk.Combobox(combobox'++id++'_frame, value=[', 0, ""); for i,item in items { owner.write('"' ++ item ++'", ', 0, ""); } owner.write(']'++justify++')'); } else { owner.write('combobox'++id++ ' = ttk.Combobox(combobox'++id++'_frame'++justify++')'); } owner.write('combobox'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('combobox'++id++ '.place_forget()'); }; if disabled { owner.write('combobox'++id++ '.configure(state="disabled")'); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('combobox'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')'); owner.write('combobox'++id++'.configure(font=combobox'++id++'_font)'); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('combobox'++id++ '.config(background="'++bgcolorStr++'")'); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('combobox'++id++ '.config(foreground="'++frcolorStr++'")'); }; owner.write('combobox'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //表格 if winctrol.cls == "listview" { var id = ctrlNameList.treeview + 1; ctrlNameList.treeview = id; owner.write('tree'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); owner.write('tree'++id++ ' = ttk.Treeview(tree'++id++'_frame)'); owner.write('tree'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('tree'++id++ '.place_forget()'); }; if disabled { owner.write('tree'++id++ '.configure(state="disabled")'); }; owner.write('tree'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //进度条 if winctrol.cls == "progress" { var id = ctrlNameList.progress + 1; ctrlNameList.progress = id; owner.write('progress'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); var max = winctrol.max; var value = winctrol.pos; owner.write('progress'++id++ ' = ttk.Progressbar(progress'++id++'_frame, value='++ value ++', maximum='++ max ++')'); owner.write('progress'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('progress'++id++ '.place_forget()'); }; if disabled { owner.write('progress'++id++ '.configure(state="disabled")'); }; owner.write('progress'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //Trackbar if winctrol.cls == "trackbar" { var id = ctrlNameList.trackbar + 1; ctrlNameList.trackbar = id; owner.write('scale'++id++'_frame = ttk.Frame(width='++ width ++', height='++ height ++')'); var min = winctrol.min; var max = winctrol.max; var value = winctrol.pos; owner.write('scale'++id++ ' = ttk.Scale(scale'++id++'_frame, from_='++ min ++', to='++ max ++', value='++ value ++ ')'); owner.write('scale'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')'); if hide { owner.write('scale'++id++ '.place_forget()'); }; if disabled { owner.write('scale'++id++ '.configure(state="disabled")'); }; owner.write('scale'++id++ '_frame.place(x='++ x ++', y='++ y ++')'); }; //GroupBox if winctrol.cls == "groupbox" { var id = ctrlNameList.groupbox + 1; ctrlNameList.groupbox = id; owner.write('labelframe'++id++ ' = ttk.Labelframe(text="'++ text ++'", width='++ width ++', height='++ height ++')'); owner.write('labelframe'++id++ '.place(x='++ x ++', y='++ y ++', width='++ width ++', height='++ height ++')'); if hide { owner.write('labelframe'++id++ '.place_forget()'); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('labelframe'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')'); owner.write('labelframe'++id++'.configure(font=labelframe'++id++'_font)'); }; }; //Frame if winctrol.cls == "custom" { var id = ctrlNameList.frame + 1; ctrlNameList.frame = id; owner.write('frame'++id++ ' = ttk.Frame(width='++ width ++', height='++ height ++')'); owner.write('frame'++id++ '.place(x='++ x ++', y='++ y ++', width='++ width ++', height='++ height ++')'); if hide { owner.write('frame'++id++ '.place_forget()'); }; }; //Tab if winctrol.cls == "tab" { var id = ctrlNameList.tab + 1; ctrlNameList.tab = id; owner.write('nb'++id++ ' = ttk.Notebook(width='++ width ++', height='++ height ++')'); owner.write('nb'++id++ '.place(x='++ x ++', y='++ y ++', width='++ width ++', height='++ height ++')'); if hide { owner.write('nb'++id++ '.place_forget()'); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('nb'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')'); owner.write('nb'++id++'.configure(font=nb'++id++'_font)'); }; }; }; owner.write('\r\n\r\n### 功能逻辑部分 ###\r\n'); owner.write('root.mainloop()'); return this.pycode; }; //转换为组件 transfer2place = function(name="SubAssembly"){ var window_width = this.width; var window_height = this.height; var window_text = this.text; this.pycode = /*** import tkinter as tk import tkinter.ttk as ttk import tkinter.font as tkFont class ***/ owner.write(name++'():\r\n ### 界面设计部分 ###\r\n \r\n def __init__(self, master):'); owner.write('self.mainframe = ttk.Frame(master, width='++ window_width ++', height='++ window_height ++')', 8); var ctrlNameList = { 'button' = 0; 'static' = 0; 'edit' = 0; 'radio' = 0; 'check' = 0; 'pic' = 0; 'listbox' = 0; 'combobox' = 0; 'progress' = 0; 'trackbar' = 0; 'listview' = 0; 'treeview' = 0; 'canvas' = 0; 'groupbox' = 0; 'frame' = 0; 'tab' = 0; }; for ( hwnd,ctrl in this.eachControlEx() ){ owner.write('# Z='++ctrl.z, 8); winctrol = this.getCtrl(hwnd); var x = winctrol.left; var y = winctrol.top; var width = winctrol.width; var height = winctrol.height; var text = winctrol.text; var hide = ctrl.hide; var disabled = ctrl.disabled; var readonly = ctrl.readonly; var bgcolor = ctrl.bgcolor; var frcolor = ctrl.color; var font = ctrl.font; var align = ctrl.align; var justify; if !align { justify = ', justify="left"'; } elseif align == "center" { justify = ', justify="center"'; } else { justify = ', justify="right"'; }; //按钮 if winctrol.cls == "button" { var id = ctrlNameList.button + 1; ctrlNameList.button = id; owner.write('self.button'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.button'++id++ ' = ttk.Button(self.button'++id++'_frame, text="'++ text ++'")', 8); owner.write('self.button'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.button'++id++ '.place_forget()', 8)}; if disabled { owner.write('self.button'++id++ '.configure(state="disabled")', 8); }; owner.write('self.button'++id++'_frame.place(x='++ x ++', y='++ y ++')', 8); }; //标签 if winctrol.cls == "static" { var id = ctrlNameList.static + 1; ctrlNameList.static = id; owner.write('self.label'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.label'++id++ ' = ttk.Label(self.label'++id++'_frame, text="'++ text ++'"'++ justify ++')', 8); owner.write('self.label'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.label'++id++ '.place_forget()', 8)}; if disabled { owner.write('self.label'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.label'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.label'++id++'.configure(font=self.label'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.label'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.label'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.label'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //文本框 if winctrol.cls == "edit" { var id = ctrlNameList.edit + 1; ctrlNameList.edit = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; owner.write('self.editVar' ++ id ++ " = tk.StringVar(value='"++ winctrol.text ++"')", 8); owner.write('self.edit'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); if winctrol.multiline == 1 { owner.write('self.edit'++id++ ' = tk.Text(self.edit'++id++'_frame)', 8); owner.write('self.edit'++id++ '.insert("end", "'++ winctrol.text ++ '")', 8); } else { owner.write('self.edit'++id++ ' = ttk.Entry(self.edit'++id++'_frame, textvariable=self.editVar'++id++ justify ++')', 8); } owner.write('self.edit'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if ctrl.password { owner.write('self.edit'++id++ '.configure(show="*")', 8); }; if hide { owner.write('self.edit'++id++ '.place_forget()', 8)}; if(readonly or disabled) { owner.write('self.edit'++id++ '.configure(state="disabled")', 8); }; if vscroll { owner.write('self.edit'++id++'_vscroll = tk.Scrollbar(self.edit'++id++', orient="vertical", command=self.edit'++id++'.yview)', 8); owner.write('self.edit'++id++'.configure(yscrollcommand=self.edit'++id++'_vscroll.set)', 8); owner.write('self.edit'++id++'_vscroll.pack(side="right", fill="y")', 8); }; if hscroll { owner.write('self.edit'++id++'_hscroll = tk.Scrollbar(self.edit'++id++', orient="horizontal", command=self.edit'++id++'.xview)', 8); owner.write('self.edit'++id++'.configure(xscrollcommand=self.edit'++id++'_hscroll.set)', 8); owner.write('self.edit'++id++'_hscroll.pack(side="bottom", fill="x")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.edit'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.edit'++id++'.configure(font=self.edit'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.edit'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.edit'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.edit'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //单选 if winctrol.cls == "radiobutton" { var id = ctrlNameList.radio + 1; ctrlNameList.radio = id; owner.write('self.radio'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.radio'++id++ ' = ttk.Radiobutton(self.radio'++id++'_frame, text="'++ text ++'"'++ justify ++')', 8); owner.write('self.radio'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.radio'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.radio'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.radio'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.radio'++id++'.configure(font=self.radio'++id++'_font)', 8); }; owner.write('self.radio'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //复选 if winctrol.cls == "checkbox" { var id = ctrlNameList.check + 1; ctrlNameList.check = id; owner.write('self.check'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.check'++id++ ' = ttk.Checkbutton(self.check'++id++'_frame, text="'++ text ++'"'++ justify ++')', 8); owner.write('self.check'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.check'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.check'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.check'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.check'++id++'.configure(font=self.check'++id++'_font)', 8); }; owner.write('self.check'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //图片框 if winctrol.cls == "picturebox" { var id = ctrlNameList.pic + 1; ctrlNameList.pic = id; owner.write('self.pic'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var picpath = winctrol.background; if picpath { owner.write('self.img' ++ id ++ " = tk.PhotoImage(file=r'"++ picpath.path ++"')", 8); owner.write('self.pic' ++ id ++ ' = ttk.Label(self.pic'++id++'_frame, image=self.img'++ id ++ justify ++')', 8); } else { owner.write('self.pic'++id++ ' = ttk.Label(self.pic'++id++'_frame'++ justify ++')', 8); } owner.write('self.pic'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.pic'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.pic'++id++ '.configure(state="disabled")', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.pic'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.pic'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.pic'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //画板 if winctrol.cls == "plus" { var id = ctrlNameList.canvas + 1; ctrlNameList.canvas = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; var bgcolor = winctrol.backgroundColor; owner.write('self.canvas'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.canvas'++id++ ' = tk.Canvas(self.canvas'++id++'_frame)', 8); owner.write('self.canvas'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.canvas'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.canvas'++id++ '.configure(state="disabled")', 8); }; if vscroll { owner.write('self.canvas'++id++'_vscroll = tk.Scrollbar(self.canvas'++id++', orient="vertical", command=self.canvas'++id++'.yview)', 8); owner.write('self.canvas'++id++'.configure(yscrollcommand=self.canvas'++id++'_vscroll.set)', 8); owner.write('self.canvas'++id++'_vscroll.pack(side="right", fill="y")', 8); }; if hscroll { owner.write('self.canvas'++id++'_hscroll = tk.Scrollbar(self.canvas'++id++', orient="horizontal", command=self.canvas'++id++'.xview)', 8); owner.write('self.canvas'++id++'.configure(xscrollcommand=self.canvas'++id++'_hscroll.set)', 8); owner.write('self.canvas'++id++'_hscroll.pack(side="bottom", fill="x")', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); bStr = ..string.slice(bgcolorStr,2,3); gStr = ..string.slice(bgcolorStr,4,5); rStr = ..string.slice(bgcolorStr,6,7); owner.write('self.canvas'++id++ '.config(background="#'++rStr++gStr++bStr++'")', 8); }; owner.write('self.canvas'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //ListBox if winctrol.cls == "listbox" { var id = ctrlNameList.listbox + 1; ctrlNameList.listbox = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; owner.write('self.list'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var items = winctrol.items; if #items>0 { owner.write('self.items'++id++' = tk.Variable(value=[', 8, ""); for i,item in items { owner.write('"' ++ item ++'", ', 0, ""); } owner.write('])', 0); owner.write('self.list'++id++ ' = tk.Listbox(self.list'++id++'_frame, listvariable=self.items'++ id ++ justify ++')', 8); } else { owner.write('self.list'++id++ ' = tk.Listbox(self.list'++id++'_frame'++ justify ++')', 8); } owner.write('self.list'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.list'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.list'++id++ '.configure(state="disabled")', 8); }; if vscroll { owner.write('self.list'++id++'_vscroll = tk.Scrollbar(self.list'++id++', orient="vertical", command=self.list'++id++'.yview)', 8); owner.write('self.list'++id++'.configure(yscrollcommand=self.list'++id++'_vscroll.set)', 8); owner.write('self.list'++id++'_vscroll.pack(side="right", fill="y")', 8); }; if hscroll { owner.write('self.list'++id++'_hscroll = tk.Scrollbar(self.list'++id++', orient="horizontal", command=self.list'++id++'.xview)', 8); owner.write('self.list'++id++'.configure(xscrollcommand=self.list'++id++'_hscroll.set)', 8); owner.write('self.list'++id++'_hscroll.pack(side="bottom", fill="x")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.list'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.list'++id++'.configure(font=self.list'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.list'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.list'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.list'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //下拉框 if winctrol.cls == "combobox" { var id = ctrlNameList.combobox + 1; ctrlNameList.combobox = id; owner.write('self.combobox'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var items = winctrol.items; if #items>0 { owner.write('self.combobox'++id++ '= ttk.Combobox(self.combobox'++id++'_frame, value=[', 8, ""); for i,item in items { owner.write('"' ++ item ++'", ', 0, ""); } owner.write(']'++ justify ++')', 0); } else { owner.write('self.combobox'++id++ ' = ttk.Combobox(self.combobox'++id++'_frame'++ justify ++')', 8); } owner.write('self.combobox'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.combobox'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.combobox'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.combobox'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.combobox'++id++'.configure(font=self.combobox'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.combobox'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.combobox'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.combobox'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //表格 if winctrol.cls == "listview" { var id = ctrlNameList.treeview + 1; ctrlNameList.treeview = id; owner.write('self.tree'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.tree'++id++ ' = ttk.Treeview(self.tree'++id++'_frame)', 8); owner.write('self.tree'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.tree'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.tree'++id++ '.configure(state="disabled")', 8); }; owner.write('self.tree'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //进度条 if winctrol.cls == "progress" { var id = ctrlNameList.progress + 1; ctrlNameList.progress = id; owner.write('self.progress'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var max = winctrol.max; var value = winctrol.pos; owner.write('self.progress'++id++ ' = ttk.Progressbar(self.progress'++id++'_frame, value='++ value ++', maximum='++ max ++')', 8); owner.write('self.progress'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.progress'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.progress'++id++ '.configure(state="disabled")', 8); }; owner.write('self.progress'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //Trackbar if winctrol.cls == "trackbar" { var id = ctrlNameList.trackbar + 1; ctrlNameList.trackbar = id; owner.write('self.scale'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var min = winctrol.min; var max = winctrol.max; var value = winctrol.pos; owner.write('self.scale'++id++ ' = ttk.Scale(self.scale'++id++'_frame, from_='++ min ++', to='++ max ++', value='++ value ++ ')', 8); owner.write('self.scale'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.scale'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.scale'++id++ '.configure(state="disabled")', 8); }; owner.write('self.scale'++id++ '_frame.place(x='++ x ++', y='++ y ++')', 8); }; //GroupBox if winctrol.cls == "groupbox" { var id = ctrlNameList.groupbox + 1; ctrlNameList.groupbox = id; owner.write('self.labelframe'++id++ ' = ttk.Labelframe(self.mainframe, text="'++ text ++'", width='++ width ++', height='++ height ++')', 8); owner.write('self.labelframe'++id++ '.place(x='++ x ++', y='++ y ++', width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.labelframe'++id++ '.place_forget()', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.labelframe'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.labelframe'++id++'.configure(font=self.labelframe'++id++'_font)', 8); }; }; //Frame if winctrol.cls == "custom" { var id = ctrlNameList.frame + 1; ctrlNameList.frame = id; owner.write('self.frame'++id++ ' = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.frame'++id++ '.place(x='++ x ++', y='++ y ++', width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.frame'++id++ '.place_forget()', 8); }; }; //Tab if winctrol.cls == "tab" { var id = ctrlNameList.tab + 1; ctrlNameList.tab = id; owner.write('self.nb'++id++ ' = ttk.Notebook(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.nb'++id++ '.place(x='++ x ++', y='++ y ++', width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.nb'++id++ '.place_forget()', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.nb'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.nb'++id++'.configure(font=self.nb'++id++'_font)', 8); }; }; }; owner.write('self.mainframe.pack()', 8); owner.write('\r\n ### 功能逻辑部分 ###\r\n\r\n', 0); return this.pycode; }; //转换为组件(pack方式) transfer2pack = function(name="SubAssembly"){ var window_width = this.width; var window_height = this.height; var window_text = this.text; this.pycode = /*** import tkinter as tk import tkinter.ttk as ttk import tkinter.font as tkFont class ***/ owner.write(name++'():\r\n ### 界面设计部分 ###\r\n \r\n def __init__(self, master):'); owner.write('self.mainframe = ttk.Frame(master, width='++ window_width ++', height='++ window_height ++')', 8); var ctrlNameList = { 'button' = 0; 'static' = 0; 'edit' = 0; 'radio' = 0; 'check' = 0; 'pic' = 0; 'listbox' = 0; 'combobox' = 0; 'progress' = 0; 'trackbar' = 0; 'listview' = 0; 'treeview' = 0; 'canvas' = 0; 'groupbox' = 0; 'frame' = 0; 'tab' = 0; }; for ( hwnd,ctrl in this.eachControlEx() ){ owner.write('# Z='++ctrl.z, 8); winctrol = this.getCtrl(hwnd); var x = winctrol.left; var y = winctrol.top; var width = winctrol.width; var height = winctrol.height; var text = winctrol.text; var hide = ctrl.hide; var disabled = ctrl.disabled; var readonly = ctrl.readonly; var bgcolor = ctrl.bgcolor; var frcolor = ctrl.color; var font = ctrl.font; var align = ctrl.align; var justify; if !align { justify = ', justify="left"'; } elseif align == "center" { justify = ', justify="center"'; } else { justify = ', justify="right"'; }; //按钮 if winctrol.cls == "button" { var id = ctrlNameList.button + 1; ctrlNameList.button = id; owner.write('self.button'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.button'++id++ ' = ttk.Button(self.button'++id++'_frame, text="'++ text ++'")', 8); owner.write('self.button'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.button'++id++ '.place_forget()', 8)}; if disabled { owner.write('self.button'++id++ '.configure(state="disabled")', 8); }; owner.write('self.button'++id++'_frame.pack()', 8); }; //标签 if winctrol.cls == "static" { var id = ctrlNameList.static + 1; ctrlNameList.static = id; owner.write('self.label'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.label'++id++ ' = ttk.Label(self.label'++id++'_frame, text="'++ text ++'"'++ justify ++')', 8); owner.write('self.label'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.label'++id++ '.place_forget()', 8)}; if disabled { owner.write('self.label'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.label'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.label'++id++'.configure(font=self.label'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.label'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.label'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.label'++id++ '_frame.pack()', 8); }; //文本框 if winctrol.cls == "edit" { var id = ctrlNameList.edit + 1; ctrlNameList.edit = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; owner.write('self.editVar' ++ id ++ " = tk.StringVar(value='"++ winctrol.text ++"')", 8); owner.write('self.edit'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); if winctrol.multiline == 1 { owner.write('self.edit'++id++ ' = tk.Text(self.edit'++id++'_frame)', 8); owner.write('self.edit'++id++ '.insert("end", "'++ winctrol.text ++ '")', 8); } else { owner.write('self.edit'++id++ ' = ttk.Entry(self.edit'++id++'_frame, textvariable=self.editVar'++id++ justify ++')', 8); } owner.write('self.edit'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if ctrl.password { owner.write('self.edit'++id++ '.configure(show="*")', 8); }; if hide { owner.write('self.edit'++id++ '.place_forget()', 8)}; if(readonly or disabled) { owner.write('self.edit'++id++ '.configure(state="disabled")', 8); }; if vscroll { owner.write('self.edit'++id++'_vscroll = tk.Scrollbar(self.edit'++id++', orient="vertical", command=self.edit'++id++'.yview)', 8); owner.write('self.edit'++id++'.configure(yscrollcommand=self.edit'++id++'_vscroll.set)', 8); owner.write('self.edit'++id++'_vscroll.pack(side="right", fill="y")', 8); }; if hscroll { owner.write('self.edit'++id++'_hscroll = tk.Scrollbar(self.edit'++id++', orient="horizontal", command=self.edit'++id++'.xview)', 8); owner.write('self.edit'++id++'.configure(xscrollcommand=self.edit'++id++'_hscroll.set)', 8); owner.write('self.edit'++id++'_hscroll.pack(side="bottom", fill="x")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.edit'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.edit'++id++'.configure(font=self.edit'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.edit'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.edit'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.edit'++id++ '_frame.pack()', 8); }; //单选 if winctrol.cls == "radiobutton" { var id = ctrlNameList.radio + 1; ctrlNameList.radio = id; owner.write('self.radio'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.radio'++id++ ' = ttk.Radiobutton(self.radio'++id++'_frame, text="'++ text ++'"'++ justify ++')', 8); owner.write('self.radio'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.radio'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.radio'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.radio'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.radio'++id++'.configure(font=self.radio'++id++'_font)', 8); }; owner.write('self.radio'++id++ '_frame.pack()', 8); }; //复选 if winctrol.cls == "checkbox" { var id = ctrlNameList.check + 1; ctrlNameList.check = id; owner.write('self.check'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.check'++id++ ' = ttk.Checkbutton(self.check'++id++'_frame, text="'++ text ++'"'++ justify ++')', 8); owner.write('self.check'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.check'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.check'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.check'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.check'++id++'.configure(font=self.check'++id++'_font)', 8); }; owner.write('self.check'++id++ '_frame.pack()', 8); }; //图片框 if winctrol.cls == "picturebox" { var id = ctrlNameList.pic + 1; ctrlNameList.pic = id; owner.write('self.pic'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var picpath = winctrol.background; if picpath { owner.write('self.img' ++ id ++ " = tk.PhotoImage(file=r'"++ picpath.path ++"')", 8); owner.write('self.pic' ++ id ++ ' = ttk.Label(self.pic'++id++'_frame, image=self.img'++ id ++ justify ++')', 8); } else { owner.write('self.pic'++id++ ' = ttk.Label(self.pic'++id++'_frame'++ justify ++')', 8); } owner.write('self.pic'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.pic'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.pic'++id++ '.configure(state="disabled")', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.pic'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.pic'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.pic'++id++ '_frame.pack()', 8); }; //画板 if winctrol.cls == "plus" { var id = ctrlNameList.canvas + 1; ctrlNameList.canvas = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; var bgcolor = winctrol.backgroundColor; owner.write('self.canvas'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.canvas'++id++ ' = tk.Canvas(self.canvas'++id++'_frame)', 8); owner.write('self.canvas'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.canvas'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.canvas'++id++ '.configure(state="disabled")', 8); }; if vscroll { owner.write('self.canvas'++id++'_vscroll = tk.Scrollbar(self.canvas'++id++', orient="vertical", command=self.canvas'++id++'.yview)', 8); owner.write('self.canvas'++id++'.configure(yscrollcommand=self.canvas'++id++'_vscroll.set)', 8); owner.write('self.canvas'++id++'_vscroll.pack(side="right", fill="y")', 8); }; if hscroll { owner.write('self.canvas'++id++'_hscroll = tk.Scrollbar(self.canvas'++id++', orient="horizontal", command=self.canvas'++id++'.xview)', 8); owner.write('self.canvas'++id++'.configure(xscrollcommand=self.canvas'++id++'_hscroll.set)', 8); owner.write('self.canvas'++id++'_hscroll.pack(side="bottom", fill="x")', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); bStr = ..string.slice(bgcolorStr,2,3); gStr = ..string.slice(bgcolorStr,4,5); rStr = ..string.slice(bgcolorStr,6,7); owner.write('self.canvas'++id++ '.config(background="#'++rStr++gStr++bStr++'")', 8); }; owner.write('self.canvas'++id++ '_frame.pack()', 8); }; //ListBox if winctrol.cls == "listbox" { var id = ctrlNameList.listbox + 1; ctrlNameList.listbox = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; owner.write('self.list'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var items = winctrol.items; if #items>0 { owner.write('self.items'++id++' = tk.Variable(value=[', 8, ""); for i,item in items { owner.write('"' ++ item ++'", ', 0, ""); } owner.write('])', 0); owner.write('self.list'++id++ ' = tk.Listbox(self.list'++id++'_frame, listvariable=self.items'++ id ++ justify ++')', 8); } else { owner.write('self.list'++id++ ' = tk.Listbox(self.list'++id++'_frame'++ justify ++')', 8); } owner.write('self.list'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.list'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.list'++id++ '.configure(state="disabled")', 8); }; if vscroll { owner.write('self.list'++id++'_vscroll = tk.Scrollbar(self.list'++id++', orient="vertical", command=self.list'++id++'.yview)', 8); owner.write('self.list'++id++'.configure(yscrollcommand=self.list'++id++'_vscroll.set)', 8); owner.write('self.list'++id++'_vscroll.pack(side="right", fill="y")', 8); }; if hscroll { owner.write('self.list'++id++'_hscroll = tk.Scrollbar(self.list'++id++', orient="horizontal", command=self.list'++id++'.xview)', 8); owner.write('self.list'++id++'.configure(xscrollcommand=self.list'++id++'_hscroll.set)', 8); owner.write('self.list'++id++'_hscroll.pack(side="bottom", fill="x")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.list'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.list'++id++'.configure(font=self.list'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.list'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.list'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.list'++id++ '_frame.pack()', 8); }; //下拉框 if winctrol.cls == "combobox" { var id = ctrlNameList.combobox + 1; ctrlNameList.combobox = id; owner.write('self.combobox'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var items = winctrol.items; if #items>0 { owner.write('self.combobox'++id++ '= ttk.Combobox(self.combobox'++id++'_frame, value=[', 8, ""); for i,item in items { owner.write('"' ++ item ++'", ', 0, ""); } owner.write(']'++ justify ++')', 0); } else { owner.write('self.combobox'++id++ ' = ttk.Combobox(self.combobox'++id++'_frame'++ justify ++')', 8); } owner.write('self.combobox'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.combobox'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.combobox'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.combobox'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.combobox'++id++'.configure(font=self.combobox'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.combobox'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.combobox'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.combobox'++id++ '_frame.pack()', 8); }; //表格 if winctrol.cls == "listview" { var id = ctrlNameList.treeview + 1; ctrlNameList.treeview = id; owner.write('self.tree'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.tree'++id++ ' = ttk.Treeview(self.tree'++id++'_frame)', 8); owner.write('self.tree'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.tree'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.tree'++id++ '.configure(state="disabled")', 8); }; owner.write('self.tree'++id++ '_frame.pack()', 8); }; //进度条 if winctrol.cls == "progress" { var id = ctrlNameList.progress + 1; ctrlNameList.progress = id; owner.write('self.progress'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var max = winctrol.max; var value = winctrol.pos; owner.write('self.progress'++id++ ' = ttk.Progressbar(self.progress'++id++'_frame, value='++ value ++', maximum='++ max ++')', 8); owner.write('self.progress'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.progress'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.progress'++id++ '.configure(state="disabled")', 8); }; owner.write('self.progress'++id++ '_frame.pack()', 8); }; //Trackbar if winctrol.cls == "trackbar" { var id = ctrlNameList.trackbar + 1; ctrlNameList.trackbar = id; owner.write('self.scale'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var min = winctrol.min; var max = winctrol.max; var value = winctrol.pos; owner.write('self.scale'++id++ ' = ttk.Scale(self.scale'++id++'_frame, from_='++ min ++', to='++ max ++', value='++ value ++ ')', 8); owner.write('self.scale'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.scale'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.scale'++id++ '.configure(state="disabled")', 8); }; owner.write('self.scale'++id++ '_frame.pack()', 8); }; //GroupBox if winctrol.cls == "groupbox" { var id = ctrlNameList.groupbox + 1; ctrlNameList.groupbox = id; owner.write('self.labelframe'++id++ ' = ttk.Labelframe(self.mainframe, text="'++ text ++'", width='++ width ++', height='++ height ++')', 8); owner.write('self.labelframe'++id++ '.pack()', 8); if hide { owner.write('self.labelframe'++id++ '.pack_forget()', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.labelframe'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.labelframe'++id++'.configure(font=self.labelframe'++id++'_font)', 8); }; }; //Frame if winctrol.cls == "custom" { var id = ctrlNameList.frame + 1; ctrlNameList.frame = id; owner.write('self.frame'++id++ ' = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.frame'++id++ '.pack()', 8); if hide { owner.write('self.frame'++id++ '.pack_forget()', 8); }; }; //Tab if winctrol.cls == "tab" { var id = ctrlNameList.tab + 1; ctrlNameList.tab = id; owner.write('self.nb'++id++ ' = ttk.Notebook(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.nb'++id++ '.pack()', 8); if hide { owner.write('self.nb'++id++ '.pack_forget()', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.nb'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.nb'++id++'.configure(font=self.nb'++id++'_font)', 8); }; }; }; owner.write('self.mainframe.pack()', 8); owner.write('\r\n ### 功能逻辑部分 ###\r\n\r\n', 0); return this.pycode; }; //转换为组件(grid方式) transfer2grid = function(name="SubAssembly"){ var window_width = this.width; var window_height = this.height; var window_text = this.text; this.pycode = /*** import tkinter as tk import tkinter.ttk as ttk import tkinter.font as tkFont class ***/ owner.write(name++'():\r\n ### 界面设计部分 ###\r\n \r\n def __init__(self, master):', 0) owner.write('self.mainframe = ttk.Frame(master, width='++ window_width ++', height='++ window_height ++')', 8); var ctrlNameList = { 'button' = 0; 'static' = 0; 'edit' = 0; 'radio' = 0; 'check' = 0; 'pic' = 0; 'listbox' = 0; 'combobox' = 0; 'progress' = 0; 'trackbar' = 0; 'listview' = 0; 'treeview' = 0; 'canvas' = 0; 'groupbox' = 0; 'frame' = 0; 'tab' = 0; }; var col = 0; var row = 0; for ( hwnd,ctrl in this.eachControlEx() ){ owner.write('# Z='++ctrl.z, 8); winctrol = this.getCtrl(hwnd); var width = winctrol.width; var height = winctrol.height; var text = winctrol.text; var hide = ctrl.hide; var disabled = ctrl.disabled; var readonly = ctrl.readonly; var bgcolor = ctrl.bgcolor; var frcolor = ctrl.color; var font = ctrl.font; var align = ctrl.align; var justify; if !align { justify = ', justify="left"'; } elseif align == "center" { justify = ', justify="center"'; } else { justify = ', justify="right"'; }; //按钮 if winctrol.cls == "button" { row++; var id = ctrlNameList.button + 1; ctrlNameList.button = id; owner.write('self.button'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.button'++id++ ' = ttk.Button(self.button'++id++'_frame, text="'++ text ++'")', 8); owner.write('self.button'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.button'++id++ '.place_forget()', 8)}; if disabled { owner.write('self.button'++id++ '.configure(state="disabled")', 8); }; owner.write('self.button'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //标签 if winctrol.cls == "static" { row++; var id = ctrlNameList.static + 1; ctrlNameList.static = id; owner.write('self.label'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.label'++id++ ' = ttk.Label(self.label'++id++'_frame, text="'++ text ++'"'++ justify ++')', 8); owner.write('self.label'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.label'++id++ '.place_forget()', 8)}; if disabled { owner.write('self.label'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.label'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.label'++id++'.configure(font=self.label'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.label'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.label'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.label'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //文本框 if winctrol.cls == "edit" { col++; var id = ctrlNameList.edit + 1; ctrlNameList.edit = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; owner.write('self.editVar' ++ id ++ " = tk.StringVar(value='"++ winctrol.text ++"')", 8); owner.write('self.edit'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); if winctrol.multiline == 1 { owner.write('self.edit'++id++ ' = tk.Text(self.edit'++id++'_frame)', 8); owner.write('self.edit'++id++ '.insert("end", "'++ winctrol.text ++ '")', 8); } else { owner.write('self.edit'++id++ ' = ttk.Entry(self.edit'++id++'_frame, textvariable=self.editVar'++id++ justify ++')', 8); } owner.write('self.edit'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if ctrl.password { owner.write('self.edit'++id++ '.configure(show="*")', 8); }; if hide { owner.write('self.edit'++id++ '.place_forget()', 8)}; if(readonly or disabled) { owner.write('self.edit'++id++ '.configure(state="disabled")', 8); }; if vscroll { owner.write('self.edit'++id++'_vscroll = tk.Scrollbar(self.edit'++id++', orient="vertical", command=self.edit'++id++'.yview)', 8); owner.write('self.edit'++id++'.configure(yscrollcommand=self.edit'++id++'_vscroll.set)', 8); owner.write('self.edit'++id++'_vscroll.pack(side="right", fill="y")', 8); }; if hscroll { owner.write('self.edit'++id++'_hscroll = tk.Scrollbar(self.edit'++id++', orient="horizontal", command=self.edit'++id++'.xview)', 8); owner.write('self.edit'++id++'.configure(xscrollcommand=self.edit'++id++'_hscroll.set)', 8); owner.write('self.edit'++id++'_hscroll.pack(side="bottom", fill="x")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.edit'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.edit'++id++'.configure(font=self.edit'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.edit'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.edit'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.edit'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //单选 if winctrol.cls == "radiobutton" { col++; var id = ctrlNameList.radio + 1; ctrlNameList.radio = id; owner.write('self.radio'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.radio'++id++ ' = ttk.Radiobutton(self.radio'++id++'_frame, text="'++ text ++'"'++ justify ++')', 8); owner.write('self.radio'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.radio'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.radio'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.radio'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.radio'++id++'.configure(font=self.radio'++id++'_font)', 8); }; owner.write('self.radio'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //复选 if winctrol.cls == "checkbox" { col++; var id = ctrlNameList.check + 1; ctrlNameList.check = id; owner.write('self.check'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.check'++id++ ' = ttk.Checkbutton(self.check'++id++'_frame, text="'++ text ++'"'++ justify ++')', 8); owner.write('self.check'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.check'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.check'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.check'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.check'++id++'.configure(font=self.check'++id++'_font)', 8); }; owner.write('self.check'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //图片框 if winctrol.cls == "picturebox" { row++; var id = ctrlNameList.pic + 1; ctrlNameList.pic = id; owner.write('self.pic'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var picpath = winctrol.background; if picpath { owner.write('self.img' ++ id ++ " = tk.PhotoImage(file=r'"++ picpath.path ++"')", 8); owner.write('self.pic' ++ id ++ ' = ttk.Label(self.pic'++id++'_frame, image=self.img'++ id ++ justify ++')', 8); } else { owner.write('self.pic'++id++ ' = ttk.Label(self.pic'++id++'_frame'++ justify ++')', 8); } owner.write('self.pic'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.pic'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.pic'++id++ '.configure(state="disabled")', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.pic'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.pic'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.pic'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //画板 if winctrol.cls == "plus" { row++; var id = ctrlNameList.canvas + 1; ctrlNameList.canvas = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; var bgcolor = winctrol.backgroundColor; owner.write('self.canvas'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.canvas'++id++ ' = tk.Canvas(self.canvas'++id++'_frame)', 8); owner.write('self.canvas'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.canvas'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.canvas'++id++ '.configure(state="disabled")', 8); }; if vscroll { owner.write('self.canvas'++id++'_vscroll = tk.Scrollbar(self.canvas'++id++', orient="vertical", command=self.canvas'++id++'.yview)', 8); owner.write('self.canvas'++id++'.configure(yscrollcommand=self.canvas'++id++'_vscroll.set)', 8); owner.write('self.canvas'++id++'_vscroll.pack(side="right", fill="y")', 8); }; if hscroll { owner.write('self.canvas'++id++'_hscroll = tk.Scrollbar(self.canvas'++id++', orient="horizontal", command=self.canvas'++id++'.xview)', 8); owner.write('self.canvas'++id++'.configure(xscrollcommand=self.canvas'++id++'_hscroll.set)', 8); owner.write('self.canvas'++id++'_hscroll.pack(side="bottom", fill="x")', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); bStr = ..string.slice(bgcolorStr,2,3); gStr = ..string.slice(bgcolorStr,4,5); rStr = ..string.slice(bgcolorStr,6,7); owner.write('self.canvas'++id++ '.config(background="#'++rStr++gStr++bStr++'")', 8); }; owner.write('self.canvas'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //ListBox if winctrol.cls == "listbox" { row++; var id = ctrlNameList.listbox + 1; ctrlNameList.listbox = id; var vscroll = ctrl.vscroll; var hscroll = ctrl.hscroll; owner.write('self.list'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var items = winctrol.items; if #items>0 { owner.write('self.items'++id++' = tk.Variable(value=[', 8, ""); for i,item in items { owner.write('"' ++ item ++'", ', 0, ""); } owner.write('])', 0); owner.write('self.list'++id++ ' = tk.Listbox(self.list'++id++'_frame, listvariable=self.items'++ id ++ justify ++')', 8); } else { owner.write('self.list'++id++ ' = tk.Listbox(self.list'++id++'_frame'++ justify ++')', 8); } owner.write('self.list'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.list'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.list'++id++ '.configure(state="disabled")', 8); }; if vscroll { owner.write('self.list'++id++'_vscroll = tk.Scrollbar(self.list'++id++', orient="vertical", command=self.list'++id++'.yview)', 8); owner.write('self.list'++id++'.configure(yscrollcommand=self.list'++id++'_vscroll.set)', 8); owner.write('self.list'++id++'_vscroll.pack(side="right", fill="y")', 8); }; if hscroll { owner.write('self.list'++id++'_hscroll = tk.Scrollbar(self.list'++id++', orient="horizontal", command=self.list'++id++'.xview)', 8); owner.write('self.list'++id++'.configure(xscrollcommand=self.list'++id++'_hscroll.set)', 8); owner.write('self.list'++id++'_hscroll.pack(side="bottom", fill="x")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.list'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.list'++id++'.configure(font=self.list'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.list'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.list'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.list'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //下拉框 if winctrol.cls == "combobox" { col++; var id = ctrlNameList.combobox + 1; ctrlNameList.combobox = id; owner.write('self.combobox'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var items = winctrol.items; if #items>0 { owner.write('self.combobox'++id++ '= ttk.Combobox(self.combobox'++id++'_frame, value=[', 8, ""); for i,item in items { owner.write('"' ++ item ++'", ', 0, ""); } owner.write(']'++ justify ++')', 0); } else { owner.write('self.combobox'++id++ ' = ttk.Combobox(self.combobox'++id++'_frame'++ justify ++')', 8); } owner.write('self.combobox'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.combobox'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.combobox'++id++ '.configure(state="disabled")', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.combobox'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.combobox'++id++'.configure(font=self.combobox'++id++'_font)', 8); }; if bgcolor { bgcolorStr = ..color.stringify(bgcolor,false); owner.write('self.combobox'++id++ '.config(background="'++bgcolorStr++'")', 8); }; if frcolor { frcolorStr = ..color.stringify(frcolor,false); owner.write('self.combobox'++id++ '.config(foreground="'++frcolorStr++'")', 8); }; owner.write('self.combobox'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //表格 if winctrol.cls == "listview" { row++; var id = ctrlNameList.treeview + 1; ctrlNameList.treeview = id; owner.write('self.tree'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.tree'++id++ ' = ttk.Treeview(self.tree'++id++'_frame)', 8); owner.write('self.tree'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.tree'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.tree'++id++ '.configure(state="disabled")', 8); }; owner.write('self.tree'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //进度条 if winctrol.cls == "progress" { row++; var id = ctrlNameList.progress + 1; ctrlNameList.progress = id; owner.write('self.progress'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var max = winctrol.max; var value = winctrol.pos; owner.write('self.progress'++id++ ' = ttk.Progressbar(self.progress'++id++'_frame, value='++ value ++', maximum='++ max ++')', 8); owner.write('self.progress'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.progress'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.progress'++id++ '.configure(state="disabled")', 8); }; owner.write('self.progress'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //Trackbar if winctrol.cls == "trackbar" { col++; var id = ctrlNameList.trackbar + 1; ctrlNameList.trackbar = id; owner.write('self.scale'++id++'_frame = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); var min = winctrol.min; var max = winctrol.max; var value = winctrol.pos; owner.write('self.scale'++id++ ' = ttk.Scale(self.scale'++id++'_frame, from_='++ min ++', to='++ max ++', value='++ value ++ ')', 8); owner.write('self.scale'++id++ '.place(x=0, y=0, width='++ width ++', height='++ height ++')', 8); if hide { owner.write('self.scale'++id++ '.place_forget()', 8); }; if disabled { owner.write('self.scale'++id++ '.configure(state="disabled")', 8); }; owner.write('self.scale'++id++'_frame.grid(column='++col++', row='++row++')', 8); }; //GroupBox if winctrol.cls == "groupbox" { row++; var id = ctrlNameList.groupbox + 1; ctrlNameList.groupbox = id; owner.write('self.labelframe'++id++ ' = ttk.Labelframe(self.mainframe, text="'++ text ++'", width='++ width ++', height='++ height ++')', 8); owner.write('self.labelframe'++id++'.grid(column='++col++', row='++row++')', 8); if hide { owner.write('self.labelframe'++id++ '.grid_forget()', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.labelframe'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.labelframe'++id++'.configure(font=self.labelframe'++id++'_font)', 8); }; }; //Frame if winctrol.cls == "custom" { row++; var id = ctrlNameList.frame + 1; ctrlNameList.frame = id; owner.write('self.frame'++id++ ' = ttk.Frame(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.frame'++id++'.grid(column='++col++', row='++row++')', 8); if hide { owner.write('self.frame'++id++ '.grid_forget()', 8); }; }; //Tab if winctrol.cls == "tab" { row++ var id = ctrlNameList.tab + 1; ctrlNameList.tab = id; owner.write('self.nb'++id++ ' = ttk.Notebook(self.mainframe, width='++ width ++', height='++ height ++')', 8); owner.write('self.nb'++id++'.grid(column='++col++', row='++row++')', 8); if hide { owner.write('self.nb'++id++ '.grid_forget()', 8); }; if font { var italic = font.italic ? 'slant="italic"' : 'slant="roman"'; var weight = font.weight==700 ? 'weight="bold"' : 'weight="normal"'; var family = 'family="'+font.name+'"'; var underline = font.underline ? 'underline=True' : 'underline=False'; var size = 'size='++..math.round(font.h*(-3)/4); owner.write('self.nb'++id++'_font = tkFont.Font(' ++ ..string.join({family; size; weight; italic; underline}, ", ") ++ ')', 8); owner.write('self.nb'++id++'.configure(font=self.nb'++id++'_font)', 8); }; }; }; owner.write('self.mainframe.pack()', 8); owner.write('\r\n ### 功能逻辑部分 ###\r\n\r\n', 0); return this.pycode; }; translateName = function(formStr){ var pattern = "(\w+)=\{.*?z=(\d\d?)\}"; var results = ..string.matches(formStr, pattern); for i, result in results { var name = result[1]; var z = result[2]; owner.write('# Z='++z++' -> '++name); }; return this.pycode; } } /**intellisense() GUI2Py = 解析aardio界面生成Python Tkinter GUI2Py.GUI2Tk(.(winform界面窗体) = 返回解析对象 ?GUI2Py.GUI2Tk = !GUI2Py_GUI2Tk. GUI2Py.GUI2Tk() = !GUI2Py_GUI2Tk. !GUI2Py_GUI2Tk.transfer2root( ) = 组件按place方式布局,返回的代码用于生成主界面 !GUI2Py_GUI2Tk.transfer2place(.( "组件名称" ) = 组件按place方式布局,返回的代码用于生成组件类 !GUI2Py_GUI2Tk.transfer2pack(.( "组件名称" ) = 组件按pack方式布局,返回的代码用于生成组件类\n生成代码后,需手动添加pack参数:\nside=['top','left','bottom','right'], fill=['x','y','both','none'] !GUI2Py_GUI2Tk.transfer2grid(.( "组件名称" ) = 组件按grid方式布局,返回的代码用于生成组件类\n生成代码后,需手动调整grid参数:\ncolumn为所在列,起始为0;row为所在行,起始为0 !GUI2Py_GUI2Tk.translateName(.( winform_DSG ) = 解析窗体设计器代码,返回的Z序与对应控件名称,可用来替换python中的控件名 end intellisense**/
import GUI2Py g3 = GUI2Py.GUI2Tk( mainForm); code = g3.transfer2assembly( "SubAss" ) /*返回的代码用于生成组件类*/
生成组件化界面
import tkinter as tk import tkinter.ttk as ttk class SubAss(): def __init__(self, master): self.SubAss = ttk.Frame(master, width=672, height=626) self.editVar1 = tk.StringVar(value='Edit') self.edit1_frame = ttk.Frame(self.SubAss, width=247, height=106) self.edit1 = tk.Text(self.edit1_frame) self.edit1.insert("end", "Edit") self.edit1.place(x=0, y=0) self.edit1_frame.place(x=200, y=191) self.SubAss.pack()
只需在窗口实例化,即可重复调用
root = tk.Tk() sa = SubAss(root) root.mainloop()
生成的界面几乎和aardio里的是一样的(相同大小和位置)
调用GUI2Python生成Python代码后,可以在### 功能逻辑 ### 代码部分添加相应需要实现的功能,例如:
### 功能逻辑部分 ### def callback(): print('callback') button1.configure(command=callback,)
这样就可以把功能绑定到按钮上,其他控件都类似。
快捷键绑定可以用bind命令,例如:
### 功能逻辑部分 ### def callback(event): print('x:', event.x, 'y:', event.y) button1.bind('<F1>', func=callback)
更新组件化界面功能,这样可以像aardio一样把不同的界面拆成单独的组件,然后在Tabs高级选项卡(python中对应的就是Notebook)进行调用。
新增了“隐藏”属性支持和背景色、前景色设置支持(仅对标签、文本框等控件有效)
新增Canvas控件,对应aardio的plus
最下面的白框是Treeview控件,对应aardio的listview,Treeview控件的宽度需要insert column后进行调节
我从github上把作者的内容搬运了过来,真得十分感谢jerryxjr开源,试用中...
import win.ui; /*DSG{{*/ mainForm = win.form(text="Window Title";right=596;bottom=383) mainForm.add( button2={cls="button";text="Button";left=242;top=261;right=369;bottom=293;z=3}; edit2={cls="edit";text="Text";left=217;top=117;right=399;bottom=234;edge=1;multiline=1;z=2}; static2={cls="static";text="Hello, Python!";left=218;top=74;right=373;bottom=103;transparent=1;z=1} ) /*}}*/ import console; console.open(); import GUI2Py; g2t = GUI2Py.GUI2Tk(mainForm); code = g2t.transfer2root(); mainForm.edit2.text = code; console.log(code); mainForm.show(); return win.loopMessage();
回复#9 @hi_aardio :
推荐尝试组件化的方式构建Tkinter,程序看起来更有层次。
例如,用aardio构建界面后,在功能逻辑部分加上要实现的功能,实例化后即可实现。
import tkinter as tk import tkinter.ttk as ttk from PIL import Image, ImageTk import numpy as np import matplotlib.pyplot as plt from io import BytesIO from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg class SubAssembly(): ### 界面设计部分 ### def __init__(self, master): self.SubAssembly = ttk.Frame(master, width=672, height=626) self.frame = ttk.Frame(self.SubAssembly, width=500, height=400) self.label = ttk.Label(self.frame, background='#00AA00') self.label.pack(fill='both') self.frame.pack(fill='both') self.SubAssembly.pack() ### 功能逻辑部分 ### # 用matplotlib画函数图,内存加载图像至Tkinter def showImg(self): x = np.linspace(2,10,50) y = np.tan(x) / np.log(x) plt.plot(x,y,'b') buff = BytesIO() plt.savefig(buff) buff.seek(0) im = Image.open(buff) img = ImageTk.PhotoImage(im) self.label.img = img self.label.config(image=img) root = tk.Tk() root.geometry("800x600+200+100") sa = SubAssembly(root) sa.showImg() root.mainloop()
实例:aardio创建界面,用matplotlib画图,实时动态显示在Tkinter中
import tkinter as tk import tkinter.ttk as ttk import matplotlib.pyplot as plt import numpy as np from io import BytesIO from PIL import Image, ImageTk class SA(): ### 界面设计部分 ### def __init__(self, master): self.mainframe = ttk.Frame(master, width=601, height=390) self.label1_frame = ttk.Frame(self.mainframe, width=209, height=27) self.label1 = ttk.Label(self.label1_frame, text="y = sin(x) / log(x)") self.label1.place(x=0, y=0) self.label1_frame.pack() self.pic1_frame = ttk.Frame(self.mainframe, width=640, height=480) self.pic1 = ttk.Label(self.pic1_frame) self.pic1.place(x=0, y=0) self.pic1_frame.pack() self.scale1_frame = ttk.Frame(self.mainframe, width=529, height=30) self.scale1 = ttk.Scale(self.scale1_frame, from_=21, to=100, value=21, command=self.drawImage) self.scale1.place(x=0, y=0) self.scale1_frame.pack() self.label2_frame = ttk.Frame(self.mainframe, width=209, height=27) self.label2_str = tk.StringVar(value='start point: x = 2.1') self.label2 = ttk.Label(self.label2_frame, textvariable=self.label2_str) self.label2.place(x=0, y=0) self.label2_frame.pack() self.mainframe.pack() ### 功能逻辑部分 ### def drawImage(self, *args): start = self.scale1.get() x = np.linspace(start, start+50, 50) x = x / 10 self.label2_str.set('start point: x = {0}'.format(start/10)) y = np.sin(x) / np.log(x) plt.clf() plt.plot(x, y, 'b') buff = BytesIO() plt.savefig(buff) buff.seek(0) im = Image.open(buff) image = ImageTk.PhotoImage(im) self.pic1.image = image self.pic1.configure(image=image) root = tk.Tk() sa = SA(root) sa.drawImage() root.mainloop()
其实,Tkinter也有各种主题,装个ttkbootstrap包后,各种bootstrap的式样都可以选。
又增加了字体属性设置,可以讲aardio中的字体属性设置到Tkinter的对应字体属性中,支持粗体、斜体、字号、下划线、字体集 5个属性。
还有字体颜色,其实字体颜色就是前景色,背景色设置中已经支持了。
新增控件的左对齐、居中、右对齐布局
多行文本框、画板、Listbox增加水平和垂直滚动条
由于遍历控件时each返回的控件顺序是随机的,所以控件名和序号不好对应。但由于控件的Z序是固定的,所以新增了translateName函数,用于解析窗体设计器代码,返回的Z序与对应控件名称,可用来在python中替换控件名(可选功能)
登录后方可回帖
20220724更新
调用
生成的python代码,界面部分可直接运行,逻辑部分需自行添加相应功能