import Widget from "../../../canvas/widgets/base" import Tools from "../../../canvas/constants/tools" import { getPythonAssetPath } from "../../utils/pythonFilePath" import { CustomTkBase } from "./base" class TopLevel extends CustomTkBase{ static widgetType = "toplevel" static displayName = "Top Level" constructor(props) { super(props) this.droppableTags = { exclude: ["image", "video", "media", "main_window", "toplevel"] } this.maxSize = { width: 2000, height: 2000 } // disables resizing above this number this.state = { ...this.state, size: { width: 450, height: 200 }, widgetName: "top level", attrs: { ...this.state.attrs, title: { label: "Window Title", tool: Tools.INPUT, // the tool to display, can be either HTML ELement or a constant string toolProps: {placeholder: "Window title", maxLength: 40}, value: "Top level", onChange: (value) => this.setAttrValue("title", value) }, logo: { label: "Toplevel Logo", tool: Tools.UPLOADED_LIST, toolProps: {filterOptions: ["image/jpg", "image/jpeg", "image/png"]}, value: "", onChange: (value) => this.setAttrValue("logo", value) } } } } componentDidMount(){ this.setAttrValue("styling.backgroundColor", "#23272D") super.componentDidMount() } generateCode(variableName, parent){ const backgroundColor = this.getAttrValue("styling.backgroundColor") const logo = this.getAttrValue("logo") const {width, height} = this.getSize() const code = [ `${variableName} = ctk.CTkToplevel(master=${parent})`, `${variableName}.configure(fg_color="${backgroundColor}")`, `${variableName}.title("${this.getAttrValue("title")}")`, `${variableName}.geometry("${width}x${height}")`, ...this.getGridLayoutConfigurationCode(variableName) ] if (logo?.name){ // code.push(`\n`) code.push(`${variableName}_img = Image.open(${getPythonAssetPath(logo.name, "image")})`) code.push(`${variableName}_img = ImageTk.PhotoImage(${variableName}_img)`) code.push(`${variableName}.iconphoto(False, ${variableName}_img)`) // code.push("\n") } return code } getImports(){ const imports = super.getImports() if (this.getAttrValue("logo")) imports.push("import os", "from PIL import Image, ImageTk", ) return imports } getRequirements(){ const requirements = super.getRequirements() if (this.getAttrValue("logo")) requirements.push("pillow") return requirements } getToolbarAttrs(){ const toolBarAttrs = super.getToolbarAttrs() return ({ widgetName: toolBarAttrs.widgetName, title: this.state.attrs.title, logo: this.state.attrs.logo, size: toolBarAttrs.size, ...this.state.attrs, }) } renderContent(){ return (
{this.getAttrValue("title")}
{this.renderTkinterLayout()}
) } } export default TopLevel