136. Creating GUI Applications with Tkinter

Creating GUI (Graphical User Interface) applications in Python is straightforward with the Tkinter module, which is part of the Python standard library. Tkinter provides a set of tools to create windows, buttons, labels, and other UI components for desktop applications.

1. Basic Window Example

This simple example demonstrates how to create a basic window using Tkinter.

Copy

import tkinter as tk

# Create the main window
window = tk.Tk()
window.title("My First Tkinter Application")

# Set the window size
window.geometry("400x300")

# Run the window
window.mainloop()

Explanation:

  • tk.Tk(): Creates the main window.

  • window.title(): Sets the title of the window.

  • window.geometry(): Specifies the dimensions of the window.

  • window.mainloop(): Keeps the window open and waits for user actions.


2. Adding Labels and Buttons

This example shows how to add a label and a button to the Tkinter window. When the button is clicked, the label text changes.

Copy

Explanation:

  • Label: A widget to display text.

  • Button: A widget that triggers a function when clicked (using the command attribute).


3. Text Entry Box

This example adds a text entry box where users can type their name and a button that greets them.

Copy

Explanation:

  • Entry: A widget for single-line text input.

  • get(): Method used to retrieve the text entered in the entry widget.


4. Using Frames for Layout Management

In this example, we use a frame to organize widgets in a structured manner.

Copy

Explanation:

  • Frame: A container widget that allows you to organize other widgets inside it.


5. Adding Checkbuttons

This example demonstrates how to add checkboxes (Checkbuttons) to the Tkinter window.

Copy

Explanation:

  • Checkbutton: A widget used to display checkboxes that can be selected or deselected.

  • IntVar(): A special type of variable used with checkbuttons to store the state (checked or unchecked).


6. Radio Buttons for Single Choice

In this example, we use radio buttons to allow users to choose one option from a set.

Copy

Explanation:

  • Radiobutton: A widget that allows the user to select one option from a list.

  • StringVar(): A special type of variable used with radio buttons to store the selected value.


7. Canvas for Drawing Shapes

This example shows how to use a canvas to draw shapes and lines in a Tkinter window.

Copy

Explanation:

  • Canvas: A widget used for drawing shapes, lines, and images.

  • create_rectangle(): Draws a rectangle on the canvas.

  • create_oval(): Draws an oval on the canvas.

  • create_line(): Draws a line on the canvas.


8. Using Messagebox for Alerts

This example uses Tkinter's built-in messagebox module to show alert dialogs.

Copy

Explanation:

  • messagebox.showinfo(): Displays an informational alert box.


9. Grid Layout

This example demonstrates the use of grid layout to organize widgets in a table-like structure.

Copy

Explanation:

  • grid(): A layout manager that places widgets in a table-like structure. Widgets are arranged by specifying their row and column positions.


These are just a few examples of what you can do with Tkinter. You can create complex, interactive applications by combining different widgets and layout options, such as menus, dialogs, and event handling.

Last updated