· 4 years ago · Aug 27, 2021, 08:00 PM
1--[[
2 Simple GUI Libray for Lua,
3 based on tkinter.
4 Wrapper for Tkinter, on Lua.
5
6
7 Converts Lua code to Python.
8 Example:
9 UI.newWindow("WindowName")
10
11 This line creates file with windowName.
12 File: WindowName.py
13
14 Writes in file:
15 "from tkinter import *\n\n\nwindow = Tk()\n"
16
17 Main class: UI.
18
19 Languages: Python, Lua.
20 Author: Ivan Perzhinsky.
21--]]
22
23UI = {} -- UI Class.
24UIWidgets = {} -- Widgets.
25
26UI.windowName = nil -- Window name.
27UI.defaultWindowIcon = "default.ico" -- Default window icon.
28UI.atExitCode = nil
29UI.undefined = nil
30
31UIWidgets.windowWidgets = {}
32
33-- Default functions of UI.
34
35-- Create new window.
36---@param windowName? string
37function UI.newWindow(windowName)
38 local newWindowInstance = io.open(windowName..".py", "w") -- Creating new window instance.
39 newWindowInstance:write("from tkinter import *\nfrom tkinter import messagebox\nfrom tkinter.ttk import Radiobutton, Checkbutton\n\nwindow = Tk()\n\n")
40 newWindowInstance:close() -- Close window instance.
41end
42
43-- Set window title.
44---@param windowName? string
45---@param windowTitle? string
46function UI.setTitle(windowName, windowTitle)
47 local windowInstance = io.open(windowName..".py", "a") -- Creating window instance.
48 windowInstance:write("window.title('"..windowTitle.."')\n")
49 windowInstance:close() -- Close window instance.
50end
51
52-- Set window geometry.
53---@param windowName? string
54---@param x? integer
55---@param y? integer
56function UI.setGeometry(windowName, x, y)
57 local windowInstance = io.open(windowName..".py", "a") -- Creating window instance.
58 windowInstance:write("window.geometry('"..tostring(x).."x"..tostring(y).."')\n")
59 windowInstance:close() -- Close window instance.
60end
61
62-- Set window resizable.
63---@param windowName? string
64---@param isResizable? boolean
65function UI.setResizable(windowName, isResizable)
66 local windowInstance = io.open(windowName..".py", "a") -- Creating window instance.
67
68 if isResizable then -- Set resizable.
69 windowInstance:write("window.resizable(True, True)\n")
70
71 elseif not isResizable then -- Set resizable to false.
72 windowInstance:write("window.resizable(False, False)\n")
73 end
74
75 windowInstance:close() -- Close window instance.
76end
77
78-- Set window icon.
79---@param windowName? string
80---@param windowIcon? string
81function UI.setWindowIcon(windowName, windowIcon)
82 local windowInstance = io.open(windowName..".py", "a") -- Creating window instance.
83 windowInstance:write("window.iconbitmap('"..windowIcon.."')\n")
84 windowInstance:close() -- Close window instance.
85end
86
87-- Run app.
88---@param windowName? string
89function UI.runWindow(windowName)
90 local windowInstance = io.open(windowName..".py", "a") -- Creating window instance.
91 windowInstance:write("\nwindow.mainloop()\n")
92 windowInstance:close() -- Close window instance.
93
94 os.execute("python "..windowName..".py") -- Executing app.
95
96 if UI.atExitCode ~= UI.undefined then
97 UI.atExitCode()
98 end
99end
100
101-- Do word at end of app.
102---@param code? string
103function UI.doWorkAtExit(code)
104 UI.atExitCode = code
105end
106
107-- Is element is list.
108---@param element? any
109---@param list? table
110function isElementInList(element, list)
111 local indexList = 1
112 local listLenght = #list
113
114 for listElement = indexList, listLenght do
115 if list[listElement] == element then
116 return true
117
118 else
119 return false
120 end
121 end
122end
123
124-- Widgets of UI.
125
126-- Label.
127---@param windowName? string
128---@param labelName? string
129---@param labelText? string
130---@param fontName? string
131---@param fontSize? integer
132---@param column? integer
133---@param row? integer
134---@param x? integer
135---@param y? integer
136function UIWidgets.newLabel(windowName, labelName, labelText, fontName, fontSize, column, row, x, y)
137 local windowInstance = io.open(windowName..".py", "a") -- Creating window instance.
138
139 if isElementInList(labelName, UIWidgets.windowWidgets) then -- If widget name exists in window.
140 error("Widget with this name is alredy exists.", 3)
141
142 else -- If widget name not exists in window.
143 table.insert(UIWidgets.windowWidgets, labelName) -- Inserting widget name.
144 windowInstance:write(labelName.." = Label(window, text='"..labelText.."', font=('"..fontName.."', "..tostring(fontSize).."))\n"..labelName..".grid(column="..tostring(column)..", row="..tostring(row)..", padx="..tostring(x)..", pady="..tostring(y)..")\n")
145 end
146 windowInstance:close() -- Close window instance.
147end
148
149-- Button.
150---@param windowName? string
151---@param buttonName? string
152---@param buttonText? string
153---@param fontName? string
154---@param fontSize? integer
155---@param column? integer
156---@param row? integer
157---@param x? integer
158---@param y? integer
159---@param onclick? string
160function UIWidgets.newButton(windowName, buttonName, buttonText, fontName, fontSize, column, row, x, y, onclick)
161 local windowInstance = io.open(windowName..".py", "a") -- Creating window instance.
162
163 if isElementInList(buttonName, UIWidgets.windowWidgets) then -- If widget name exists in window.
164 error("Widget with this name is alredy exists.", 3)
165
166 else -- If widget name not exists in window.
167 table.insert(UIWidgets.windowWidgets, buttonName) -- Inserting widget name.
168 windowInstance:write(buttonName.." = Button(window, text='"..buttonText.."', font=('"..fontName.."', "..tostring(fontSize).."), command=lambda: exec('"..onclick.."'))\n"..buttonName..".grid(column="..tostring(column)..", row="..tostring(row)..", padx="..tostring(x)..", pady="..tostring(y)..")\n")
169 end
170 windowInstance:close() -- Close window instance.
171end
172
173-- Entry.
174---@param windowName? string
175---@param entryName? string
176---@param width? integer
177---@param column? integer
178---@param row? integer
179---@param x? integer
180---@param y? integer
181function UIWidgets.newEntry(windowName, entryName, width, column, row, x, y)
182 local windowInstance = io.open(windowName..".py", "a") -- Creating window instance.
183
184 if isElementInList(entryName, UIWidgets.windowWidgets) then -- If widget name exists in window.
185 error("Widget with this name is alredy exists.", 3)
186
187 else
188 table.insert(UIWidgets.windowWidgets, entryName)
189 windowInstance:write(entryName.." = Entry(window, width="..tostring(width)..")\n"..entryName..".grid(column="..tostring(column)..", row="..tostring(row)..", padx="..tostring(x)..", pady="..tostring(y)..")\n")
190 end
191
192 windowInstance:close() -- Close window instance.
193end
194
195-- Get widgets name.
196function UIWidgets.getWidgetsFromWindow()
197 local indexList = 1
198 local widgets = ""
199
200 for widget = indexList, #UIWidgets.windowWidgets do
201 if UIWidgets.windowWidgets[widget] == UIWidgets.windowWidgets[#UIWidgets.windowWidgets] then
202 widgets = widgets..UIWidgets.windowWidgets[widget]
203
204 else
205 widgets = widgets..UIWidgets.windowWidgets[widget].."\n"
206 end
207 end
208
209 return widgets
210end
211
212-- Add user-code.
213---@param windowSource? string
214---@param code? string
215function addCode(windowSource, code)
216 local windowInstance = io.open(windowSource..".py", "a") -- Creating window instance.
217 windowInstance:write(code) -- User code.
218 windowInstance:close() -- Close window instance.
219end
220