vivicai / aardioGUI2Python

Transfer an aardio GUI to TKinter GUI of Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

aardioGUI2Python

GUI2Python Lib - use aardio to create UI of Tkinter and generate Python code. - As a Tkinter Designer

TKinter is a native Python Lib to easily create GUI of Python program,but there is no visual interface to use. Not friendly at all!

Not too complicated, just like a "Qt Designer".

aardio is very easy to use to create UI so that it can be nice to create UI in aardio and transfer to tkinter in Python automatically!


GUI2Python库 - 用aardio画Tkinter界面并生成Python代码 - As a Tkinter Designer

TKinter是Python的原生GUI库,用来创建一些简单的GUI界面还是很方便的。但是,缺点是没有提供一套图形化的设计界面,这样创建界面的时候就非常不直观。

思路是搞一个类似QtDesigner类似的工具,但是又不想搞得太复杂。

aardio的图形化设计非常便捷,这样在aardio里画好界面自动生成tkinter的python代码就是再好不过的事情了。


目前可以实现的功能:

  • 20220725更新:
  1. 所有可禁用控件,增加禁用属性
  2. 文本框增加密码属性
  3. 增加pack布局
  4. 增加grid布局
  • 20220724更新:
  1. 基本控件都已经完善,包括Label, Button, Text/Entry, RadioButton, CheckBox, PictureBox, Canvas, ListBox, ComboBox, Treeview, ProgressBar, Scale, LabelFrame, Frame, Notebook等,基本满足常规使用。其余Tkinter支持的控件也可以通过手动方式添加使用。
  2. 可以把界面直接转换为主界面
  3. 更新组件化界面功能,这样可以像aardio一样把不同的界面拆成单独的组件,然后在Tabs高级选项卡(python中对应的就是Notebook)进行调用。也可以嵌入到其他Frame、Notebook、LabelFrame等容器中,当然也可以是主窗口Tk容器。
  4. 新增了“隐藏”属性支持和背景色、前景色设置支持(仅对标签、文本框等控件有效)
  5. 新增Canvas控件,对应aardio的plus

Quick Start

  1. 在aardio中画界面
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}
)
/*}}*/

mainForm.show();
return win.loopMessage();
  1. 调用
import GUI2Py;
g2t = GUI2Py.GUI2Tk(mainForm);
code = g2t.transfer2root();

生成的code就是我们所需要的python代码,可以直接复制到python运行。

  1. python代码
import tkinter as tk
import tkinter.ttk as ttk
   
root = tk.Tk()
   
### 界面设计部分 ###
   
root.geometry("601x390")
root.title("Window Title")
button1_frame = ttk.Frame(width=127, height=32)
button1 = ttk.Button(button1_frame, text="Button")
button1.place(x=0, y=0)
button1_frame.place(x=242, y=261)
label1_frame = ttk.Frame(width=155, height=29)
label1 = ttk.Label(label1_frame, text="Hello, Python!")
label1.place(x=0, y=0)
label1_frame.place(x=218, y=74)
editVar1 = tk.StringVar(value='Text')
edit1_frame = ttk.Frame(width=182, height=117)
edit1 = tk.Text(edit1_frame)
edit1.insert("end", "Text")
edit1.place(x=0, y=0)
edit1_frame.place(x=217, y=117)


### 功能逻辑部分 ###

root.mainloop()

aardioGUI2Python库可以转换大部分的常用控件至Python的Tkinter中,界面可以直接运行。当然“功能逻辑部分”需要自行添加,可以实现预想的功能。

进阶应用

把界面组件化,这样可以像aardio一样把不同的界面拆成单独的组件,然后在Tabs高级选项卡(python中对应的就是Notebook)进行调用。也可以嵌入到其他Frame、Notebook、LabelFrame等容器中,当然也可以是主窗口Tk容器。

import GUI2Py;
g2t = GUI2Py.GUI2Tk(mainForm);
code = g2t.transfer2assembly();

生成的python代码

import tkinter as tk
import tkinter.ttk as ttk
  
class SubAssembly():
    ### 界面设计部分 ###
    
    def __init__(self, master):
        self.mainframe = ttk.Frame(master, width=601, height=390)
        self.label1_frame = ttk.Frame(self.mainframe, width=155, height=29)
        self.label1 = ttk.Label(self.label1_frame, text="Hello, Python!")
        self.label1.place(x=0, y=0)
        self.label1_frame.place(x=218, y=74)
        self.editVar1 = tk.StringVar(value='Text')
        self.edit1_frame = ttk.Frame(self.mainframe, width=182, height=117)
        self.edit1 = tk.Text(self.edit1_frame)
        self.edit1.insert("end", "Text")
        self.edit1.place(x=0, y=0)
        self.edit1_frame.place(x=217, y=117)
        self.button1_frame = ttk.Frame(self.mainframe, width=127, height=32)
        self.button1 = ttk.Button(self.button1_frame, text="Button")
        self.button1.place(x=0, y=0)
        self.button1_frame.place(x=242, y=261)
        self.mainframe.pack()

    ### 功能逻辑部分 ###

在python中调用,只需要实例化即可,当然如果需要实现额外功能,需要自行添加功能代码。

root = tk.Tk()
sa = SubAssembly(root)
root.mainloop()

应用实例

  • aardio创建界面,用matplotlib画图,实时动态显示在Tkinter中

https://www.htmlayout.cn/upload/image/20220726/1658814090451434.gif

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()

About

Transfer an aardio GUI to TKinter GUI of Python

License:BSD 2-Clause "Simplified" License