working on code generation
This commit is contained in:
33
src/frameworks/tkinter/engine/code.js
Normal file
33
src/frameworks/tkinter/engine/code.js
Normal file
@@ -0,0 +1,33 @@
|
||||
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")
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default generateTkinterCode
|
||||
@@ -1,16 +1,46 @@
|
||||
import { Layouts, PosType } from "../../../canvas/constants/layouts"
|
||||
import Tools from "../../../canvas/constants/tools";
|
||||
import Widget from "../../../canvas/widgets/base";
|
||||
import Tools from "../../../canvas/constants/tools"
|
||||
import Widget from "../../../canvas/widgets/base"
|
||||
|
||||
|
||||
|
||||
class TkinterBase extends Widget {
|
||||
|
||||
|
||||
setParentLayout(layout){
|
||||
|
||||
static requiredImports = ['import tkinter as tk']
|
||||
|
||||
constructor(props) {
|
||||
super(props)
|
||||
|
||||
this.getLayoutCode = this.getLayoutCode.bind(this)
|
||||
}
|
||||
|
||||
getLayoutCode(){
|
||||
const {layout: parentLayout, direction, gap} = this.getParentLayout()
|
||||
|
||||
let layoutManager = `pack()`
|
||||
|
||||
if (parentLayout === Layouts.FLEX){
|
||||
layoutManager = `pack(${direction === "horizontal"? "tk.LEFT" : "tk.TOP"})`
|
||||
}else if (parentLayout === Layouts.GRID){
|
||||
const row = this.getAttrValue("gridManager.row")
|
||||
const col = this.getAttrValue("gridManager.col")
|
||||
layoutManager = `grid(row=${row}, col=${col})`
|
||||
}else{
|
||||
// FIXME: position may not be correct
|
||||
layoutManager = `place(x=${this.state.pos.x}, y=${this.state.pos.y})`
|
||||
}
|
||||
|
||||
return layoutManager
|
||||
}
|
||||
|
||||
setParentLayout(parentLayout){
|
||||
console.log("parent layout: ", parentLayout)
|
||||
|
||||
const {layout, direction, gap} = parentLayout
|
||||
|
||||
// show attributes related to the layout manager
|
||||
let updates = {
|
||||
parentLayout: layout,
|
||||
parentLayout: parentLayout,
|
||||
}
|
||||
|
||||
this.removeAttr("gridManager")
|
||||
@@ -21,7 +51,7 @@ class TkinterBase extends Widget {
|
||||
positionType: PosType.NONE
|
||||
}
|
||||
|
||||
if (layout === Layouts.GRID) {
|
||||
if (parentLayout === Layouts.GRID) {
|
||||
// Set attributes related to grid layout manager
|
||||
updates = {
|
||||
...updates,
|
||||
@@ -145,14 +175,14 @@ class TkinterBase extends Widget {
|
||||
parentLayout: parentLayout
|
||||
}
|
||||
|
||||
if (parentLayout === Layouts.FLEX || parentLayout === Layouts.GRID){
|
||||
if (parentLayout.layout === Layouts.FLEX || parentLayout.layout === Layouts.GRID){
|
||||
|
||||
layoutUpdates = {
|
||||
...layoutUpdates,
|
||||
positionType: PosType.NONE
|
||||
}
|
||||
|
||||
}else if (parentLayout === Layouts.PLACE){
|
||||
}else if (parentLayout.layout === Layouts.PLACE){
|
||||
layoutUpdates = {
|
||||
...layoutUpdates,
|
||||
positionType: PosType.ABSOLUTE
|
||||
|
||||
@@ -10,7 +10,7 @@ class Frame extends TkinterBase{
|
||||
super(props)
|
||||
|
||||
this.droppableTags = {
|
||||
exclude: ["image", "video", "media", "toplevel"]
|
||||
exclude: ["image", "video", "media", "toplevel", "main_window"]
|
||||
}
|
||||
|
||||
this.state = {
|
||||
|
||||
@@ -2,6 +2,7 @@ import Widget from "../../../canvas/widgets/base"
|
||||
import Tools from "../../../canvas/constants/tools"
|
||||
import { removeKeyFromObject } from "../../../utils/common"
|
||||
import TkinterBase from "./base"
|
||||
import { Layouts } from "../../../canvas/constants/layouts"
|
||||
|
||||
|
||||
class Label extends TkinterBase{
|
||||
@@ -45,6 +46,16 @@ class Label extends TkinterBase{
|
||||
}
|
||||
}
|
||||
|
||||
generateCode(parent){
|
||||
|
||||
const label = this.getAttrValue("labelWidget")
|
||||
|
||||
return (`
|
||||
${this.getWidgetName()} = tk.Label(master=${parent}, text="${label}")
|
||||
${this.getWidgetName()}.${this.getLayoutCode()}
|
||||
`)
|
||||
}
|
||||
|
||||
componentDidMount(){
|
||||
super.componentDidMount()
|
||||
this.setAttrValue("styling.backgroundColor", "#fff0")
|
||||
|
||||
@@ -36,6 +36,13 @@ class MainWindow extends Widget{
|
||||
this.setWidgetName("main")
|
||||
}
|
||||
|
||||
generateCode(parent){
|
||||
|
||||
return (`
|
||||
${this.getWidgetName()} = tk.Tk()
|
||||
`)
|
||||
}
|
||||
|
||||
getToolbarAttrs(){
|
||||
const toolBarAttrs = super.getToolbarAttrs()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user