import { createFilesAndDownload } from "../../../codeEngine/utils" import MainWindow from "../widgets/mainWindow" import { message } from "antd" async function generateTkinterCode(projectName, widgetList=[], widgetRefs=[]){ console.log("widgetList and refs", projectName, widgetList, widgetRefs) let mainWindowCount = 0 for (let widget of widgetList){ if (widget.widgetType === MainWindow){ mainWindowCount += 1 } if (mainWindowCount > 1){ message.error("Multiple instances of Main window found, delete one and try again.") return } } if (mainWindowCount === 0){ message.error("Aborting. No instances of Main window found. Add one and try again") return } let variableNames = [] // {widgetId: "", name: "", count: 1} let imports = new Set([]) let requirements = new Set([]) let code = [`# This code is generated by PyUIbuilder: https://github.com/paulledemon \n`] code.push("\n", "import tkinter as tk", "\n\n") let mainVariable = "" // the main window variable for (let widget of widgetList){ console.log("Key: ", widget) const widgetRef = widgetRefs[widget.id].current let varName = widgetRef.getVariableName() imports.add(widgetRef.getImports()) requirements.add(widgetRef.getRequirements()) if (widget.widgetType === MainWindow){ mainVariable = varName } if (!variableNames.find((val) => val.variable === varName)){ variableNames.push({widgetId: widgetRef.id, name: varName, count: 1}) }else{ // Avoid duplicate names const existingVariable = variableNames.find((val) => val.variable === varName) const existingCount = existingVariable.count existingVariable.count += 1 varName = `${existingVariable.name}${existingCount}` variableNames.push({widgetId: widgetRef.id, name: varName, count: 1}) } const parentVariable = variableNames.find(val => val.widgetId === widget.id)?.name || null console.log("widget ref: ", widgetRef) let widgetCode = widgetRef.generateCode(varName, parentVariable) if (!widgetCode instanceof Array){ throw new Error("Generate code function should return array, each new line should be a new item") } // add \n after every line widgetCode = widgetCode.flatMap((item, index) => index < code.length - 1 ? [item, "\n"] : [item]) code.push(...widgetCode) code.push("\n") } code.push(`\n${mainVariable}.mainloop()`) // start the main loop for tkinter console.log("Code: ", code.join("")) message.info("starting zipping files, download will start in a few seconds") // createFilesAndDownload([], projectName).then(() => { // message.success("Download complete") // }).catch(() => { // message.error("Error while downloading") // }) } export default generateTkinterCode