php-tkui

repository·master·Indexed 19 days ago

https://github.com/skoro/php-tkui

A PHP library for building desktop UI applications by leveraging the PHP FFI extension to communicate with the Tcl/Tk toolkit. It provides tools for managing Tcl interpreters, executing Tcl scripts, registering custom PHP commands, and managing GUI widgets and themes. Requires PHP >= 8.2, the ffi extension, and Tcl/Tk >= 8.6.

Tokens
10.7K
Snippets
41
Records
56
Agent score
67%

What's inside php-tkui

  1. Configure Tcl/Tk paths on Windows

    master

    On Windows, you must install a Tcl/Tk binary distribution and explicitly tell php-tkui where the DLL files are located by updating your .env file.

    WINDOWS_LIB_TCL=c:\\tcltk\\bin\\tcl86t.dll
    WINDOWS_LIB_TK=c:\\tcltk\\bin\\tk86t.dll
  2. Build Tcl and Tk for Windows using MSYS2

    master

    To build Tcl and Tk for Windows, use the MSYS2 environment with the UCRT64 toolchain. This process involves installing dependencies via pacman, navigating to the project's tools directory, and executing the make command with the OS=win environment variable.

    Prerequisites

    1. Install MSYS2 from https://www.msys2.org/.
    2. Launch the MSYS2 UCRT64 terminal.

    Build Steps

    1. Update the system and install build dependencies:
      pacman -Syuu
      pacman -S mingw-w64-ucrt-x86_64-gcc make git zip wget
    2. Navigate to the project's tools directory.
    3. Run the build command:
      OS=win make dist

    Output

    The resulting tcltk.zip file will be located in the .build directory.

    # Install dependencies
    pacman -Syuu
    pacman -S mingw-w64-ucrt-x86_64-gcc make git zip wget
    
    # Build
    cd tools
    OS=win make dist
  3. Install php-tkui and run demos

    master

    To get started with php-tkui, ensure you have the requirements met, then clone the repository and use Composer to install dependencies. You can run the included demos to verify your installation.

    Prerequisites

    • PHP >= 8.2
    • ffi extension enabled
    • Tcl/Tk >= 8.6 installed on your OS

    Installation Steps

    1. Clone the repository.
    2. Install dependencies via Composer.
    3. Run a demo script (e.g., demos/buttons.php).
    git clone https://github.com/skoro/php-tkui.git php-tkui
    cd php-tkui
    composer install
    php demos/buttons.php
  4. Configure Tcl/Tk paths on macOS

    master

    On macOS, install Tcl/Tk (e.g., via brew install tcl-tk) and then specify the paths to the .dylib files in your .env file.

    brew install tcl-tk
    DARWIN_LIB_TCL=/usr/local/Cellar/tcl-tk/[installed_version]/lib/libtcl8.6.dylib
    DARWIN_LIB_TK=/usr/local/Cellar/tcl-tk/[installed_version]/lib/libtk8.6.dylib
  5. Configure php-tkui via .env

    master

    Configuration is managed through a .env file. Copy .env.example to .env to begin customizing your application settings.

    Debug Mode

    Enabling debug mode allows you to see which commands are being executed by the Tcl engine. Set DEBUG=true and specify a destination for logs using DEBUG_LOG (e.g., php://stdout for console output or a file path).

    Application Appearance (Themes)

    Use the THEME option to set the visual style of your application.

    • Default: THEME=auto (selects a theme based on your OS).
    • Cross-platform themes: clam, alt, default, classic.
    • Windows-specific themes: winnative, xpnative, vista.
    DEBUG=true
    DEBUG_LOG=php://stdout
    THEME=auto
  6. Initialize and run a TkApplication

    master

    To create a GUI application using TkApplication, you must first instantiate it with a Tk instance. You then call init() to initialize the Tcl/Tk libraries and the ttk package (if available), and finally call run() to start the main event loop. To close the application and clean up widgets, use quit().

    Note that init() handles the setup of the Tcl interpreter, argv arguments, and the ttk theme manager.

    // Assuming $tk is an instance of Tk
    $app = new TkApplication($tk, ['arg1' => 'val1']);
    $app->init();
    
    // ... build your UI ...
    
    $app->run();
  7. Configure Text widget options

    master

    When instantiating a Text widget, you can pass an array of options to customize its behavior and appearance. Supported keys include:

    • autoSeparators: (null)
    • blockCursor: (null)
    • endLine: (null)
    • height: (null)
    • inactiveSelectBackground: (null)
    • insertUnfocussed: (null)
    • font: (null)
    • maxUndo: (null)
    • spacing1: (null)
    • spacing2: (null)
    • spacing3: (null)
    • startLine: (null)
    • state: (null)
    • tabs: (null)
    • tabStyle: (null)
    • undo: (null)
    • width: (null)
    • wrap: (null)
  8. Configure Listbox options

    master

    When creating a Listbox, you can pass the following options in the $options array:

    OptionTypeDescription
    activeStyleActiveStyleThe style applied to the currently active item.
    heightintThe height of the listbox in lines.
    listVariablestringThe Tcl variable associated with the list.
    selectModeSelectModeThe mode for selecting items (e.g., single, multiple).
    stateStateThe current state of the widget.
    widthintThe width of the listbox in characters.
  9. Configure Notebook widget options

    master

    The Notebook widget supports the following configuration options via TclOptions:

    OptionTypeDescription
    heightintThe height of the notebook widget.
    widthintThe width of the notebook widget.
    paddingstringThe padding for the notebook widget.

    Note: The implementation suggests padding may eventually require a list of integers.

  10. Execute raw Tcl commands with tclEval()

    master

    The tclEval(...$args) method allows you to execute arbitrary Tcl commands directly within the application's interpreter. It automatically handles argument quoting for strings and arrays to ensure valid Tcl syntax.

    Returns the string result of the evaluated Tcl command.

    $result = $app->tclEval('set', 'x', '10');
    // or
    $result = $app->tclEval('tk', 'windowingsystem');
  11. Create and populate a Menu

    master

    The Tkui\Widgets\Menu\Menu class is used to create menu bars and dropdown menus. You can add submenus, individual menu items, separators, or groups of items to a Menu instance.

    Key Methods:

    • addMenu(string $title): Creates and returns a new Menu instance as a submenu (cascade) under the current menu. Supports underline characters for keyboard shortcuts.
    • addItem(CommonItem $item): Adds a single menu item (like MenuItem) to the menu.
    • addSeparator(): Adds a visual separator between items.
    • addGroup(CommonGroup $group): Adds a collection of menu items at once.

    When constructing a Menu, you can pass the following options:

    • postCommand: A callable executed when the menu is displayed.
    • selectColor: A Color or string defining the selection color.
    • title: The title of the menu.
    use Tkui//...; 
    use Tkui//...;
    
    $menu = new Menu($parent, [
        'title' => 'File',
        'selectColor' => 'blue',
    ]);
    
    // Adding a submenu
    $fileMenu = $menu->addMenu("File");
    
    // Adding a single item
    $fileMenu->addItem(new MenuItem('Open', function() {
        // Handle open action
    }));
    
    // Adding a separator
    $fileMenu->addSeparator();
  12. Handle tab change events with onChanged()

    master

    You can react to users switching between tabs by using the onChanged() method. This method accepts a callback that is executed whenever the selected tab changes.

    The callback receives two arguments:

    1. The NotebookTab instance that was just selected.
    2. The Notebook widget instance.
    $notebook->onChanged(function (NotebookTab $tab, Notebook $nb) {
        echo "Selected tab: " . $tab->title(); // Assuming title() exists on NotebookTab
    });
    $notebook->onChanged(function (NotebookTab $tab, Notebook $nb) {
        // Logic to execute when a tab is selected
    });