This guide walks you through creating a basic GTK application using React GTK. You’ll learn how to set up a simple window with UI components and handle user interactions.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/devhammed/react-gtk/llms.txt
Use this file to discover all available pages before exploring further.
Overview
A basic React GTK application consists of:- A
GtkWindowas the top-level container - Layout components like
GtkBoxto organize widgets - UI widgets like
GtkLabelandGtkButton - React state management for interactivity
Set up the application entry point
Create your main entry file that initializes the GTK application:The
index.tsx
createRoot function initializes the React GTK renderer, similar to React DOM’s createRoot.Create a basic window component
Build your main application component with a window, label, and button:
app.tsx
Understand the component structure
GtkWindow: The top-level window container
defaultHeightanddefaultWidth: Set the initial window sizetitle: The window title displayed in the title bar
orientation: UseGtkOrientation.VERTICALorGtkOrientation.HORIZONTALspacing: Space between child widgetsmarginStartandmarginEnd: Padding around the boxvalignandhalign: Vertical and horizontal alignment
label: The text content to display
onClicked: Event handler when the button is clicked- Children: The button label (can be text or other components)
Adding Conditional Rendering
You can use React’s conditional rendering with GTK components:React GTK supports standard React features like hooks, conditional rendering, and state management. You can use
useState, useEffect, useMemo, and other React hooks just like in React DOM applications.Key Concepts
Layout and Alignment
Layout and Alignment
GTK uses a box model for layout. The
GtkBox component is fundamental for arranging widgets:- Vertical orientation: Stacks children from top to bottom
- Horizontal orientation: Arranges children from left to right
- Alignment: Control positioning with
valign(top, center, bottom) andhalign(start, center, end) - Spacing: Add gaps between children with the
spacingproperty
Event Handling
Event Handling
GTK widgets emit signals that you can handle with React props:
GtkButtonusesonClickedfor click events- Event handlers receive the widget instance as an argument
- You can update React state in event handlers to trigger re-renders
Window Properties
Window Properties
The
GtkWindow component accepts several properties:title: Window title textdefaultWidthanddefaultHeight: Initial window dimensionsmodal: Make the window modal (blocks interaction with other windows)resizable: Whether the window can be resized (default: true)
Next Steps
- Learn how to manage multiple windows
- Explore form inputs with GtkEntry
- Check out the component reference for all available widgets
