API Reference

This section provides a reference for the core classes and modules within the Tkinter Designer source code. This is intended for developers who wish to understand the project's architecture or contribute to its development.

Core Module: tkdesigner.designer

This module contains the main Designer class that orchestrates the entire code generation process.

class Designer

The central class that handles the interaction with the Figma API and manages the generation of code and assets.

__init__(self, token: str, file_key: str, output_path: Path)

  • token: Your Figma Personal Access Token.
  • file_key: The unique key of the Figma file, extracted from the file's URL.
  • output_path: A pathlib.Path object pointing to the directory where the build folder will be created.

to_code(self) -> list[str]

  • Processes the Figma file data, iterates through all top-level frames (canvases), and generates the Python code for each one.
  • Returns: A list of strings, where each string is the complete Python code for a single frame (GUI window).

design(self)

  • This is the main public method that executes the entire workflow.
  • It calls to_code() to generate the code, and then writes each code string to a .py file (e.g., gui.py, gui1.py, etc.) within the specified output path.

Figma Processing Modules: tkdesigner.figma

This package is responsible for representing Figma objects as Python classes and processing them.

class tkdesigner.figma.frame.Frame

Represents a top-level frame from Figma, which corresponds to a single Tkinter window.

  • __init__(self, node, figma_file, output_path, frameCount=0): Initializes the frame with its corresponding Figma node data. It sets up the output paths for assets specific to this frame.
  • create_element(self, element): The factory method that takes a Figma element node (e.g., a rectangle, text, group) and returns the appropriate Python object representation (e.g., Button, Text, Image).
  • to_code(self, template): Renders the final Python code for this frame and all its child elements using a Jinja2 template.

Element Classes in tkdesigner.figma.custom_elements

These classes represent the specific Tkinter widgets that can be generated. Each class inherits from a base Vector class and contains the logic to generate its own code snippet.

  • class Button(Rectangle): Represents a button. It handles downloading its own image asset and generates the tkinter.Button widget code.
  • class Text(Vector): Represents static text on the canvas. It generates the canvas.create_text() call.
  • class Image(Vector): Represents a static image. It handles downloading the image and generates the canvas.create_image() call.
  • class TextEntry(Vector): Represents both TextBox (single-line Entry) and TextArea (multi-line Text). It generates the widget code and the background image for the entry field.
  • class ButtonHover(Rectangle): An experimental class to handle hover effects for buttons by swapping the button's image.

API Endpoint Module: tkdesigner.figma.endpoints

  • class Files: A utility class to abstract the API calls to Figma's /v1/files and /v1/images endpoints. It handles making the requests calls with the appropriate headers and parameters.