---
name: "Tk_Main"
section: "3"
platform: "linux"
package: "tk"
version: "8.6.16-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "macos"]
url: "https://new.carta.tech/Tk_Main/3"
---

# Tk\_Main(3)

main program for Tk-based applications

```

```

## NAME

Tk\_Main - main program for Tk-based applications

## SYNOPSIS

```
#include <tk.h>

Tk_Main(argc, argv, appInitProc)
```

## ARGUMENTS

- int *argc* (in)

  Number of elements in *argv*.

- char *\*argv\[\]* (in)

  Array of strings containing command-line arguments. On Windows, when using -DUNICODE, the parameter type changes to wchar\_t \*.

- Tcl\_AppInitProc *\*appInitProc* (in)

  Address of an application-specific initialization procedure. The value for this argument is usually **Tcl\_AppInit**.

  ```

  ```

## DESCRIPTION

**Tk\_Main** acts as the main program for most Tk-based applications. Starting with Tk 4.0 it is not called **main** anymore because it is part of the Tk library and having a function **main** in a library (particularly a shared library) causes problems on many systems. Having **main** in the Tk library would also make it hard to use Tk in C++ programs, since C++ programs must have special C++ **main** functions.

Normally each application contains a small **main** function that does nothing but invoke **Tk\_Main**. **Tk\_Main** then does all the work of creating and running a **wish**-like application.

When it is has finished its own initialization, but before it processes commands, **Tk\_Main** calls the procedure given by the *appInitProc* argument.  This procedure provides a “hook” for the application to perform its own initialization, such as defining application-specific commands.  The procedure must have an interface that matches the type **Tcl\_AppInitProc**:

```
typedef int Tcl_AppInitProc(
        Tcl_Interp *interp);
```

*AppInitProc* is almost always a pointer to **Tcl\_AppInit**; for more details on this procedure, see the documentation for **Tcl\_AppInit**.

**Tk\_Main** functions much the same as **Tcl\_Main**.  In particular, **Tk\_Main** supports both an interactive mode and a startup script mode, with the file name and encoding of a startup script under the control of the **Tcl\_SetStartupScript** and **Tcl\_GetStartupScript** routines. However it calls **Tk\_MainLoop** after processing any supplied script, and in interactive uses events registered with **Tcl\_CreateFileHandler** to process user input.

## SEE ALSO

[Tcl\_DoOneEvent(3)](/Tcl_DoOneEvent/3.md)

## KEYWORDS

application-specific initialization, command-line arguments, main program
