Creating a GUI in Python

Tkinter is the standard library for creating GUIs in Python. It provides an easy and convenient way to create windowed applications using widgets such as buttons, labels, input fields and more. In this article, we will look at the basic concepts of Tkinter and create a simple application.

Installation and configuration

Tkinter comes bundled with Python, even in IDLE, so no additional libraries need to be installed to use it. To make sure Tkinter is installed, you can run the following code:

import tkinter as tk
print(tk.TkVersion)

If you see the Tkinter version, then everything is configured correctly.

Basic Tkinter Concepts

Creating a Window

The first step in creating an application using Tkinter is to create a main window. This is done using the Tk class.

import tkinter as tk
root = tk.Tk()
root.title("Мое первое окно")
root.geometry("400x300")
root.mainloop()

You will see a small empty window.

Widgets

Widgets are basic interface elements such as buttons, labels, input fields, etc. In Tkinter, widgets are created and placed in a window using various methods.

Labels

Labels are used to display text or images.

label = tk.Label(root, text="Привет, мир!")
label.pack

Buttons

Buttons are used to perform actions when pressed.

def on_button_click():
    print("Кнопка нажата!")

button = tk.Button(root, text="Нажми меня", command=on_button_click)
button.pack()

Entries

Input fields are used to enter text.

entry = tk.Entry(root)
entry.pack()

Placing widgets

There are several ways to place widgets in a window in Tkinter:

  • pack() is a simple way to stack widgets.

  • grid() is a more flexible way to place widgets in a table.

  • place() – placement of widgets in absolute coordinates.

grid() example

label1 = tk.Label(root, text="Имя:")
label1.grid(row=0, column=0)

entry1 = tk.Entry(root)
entry1.grid(row=0, column=1)

label2 = tk.Label(root, text="Фамилия:")
label2.grid(row=1, column=0)

entry2 = tk.Entry(root)
entry2.grid(row=1, column=1)

Sample Application

Now let's create a simple application that will prompt for a username and display a welcome message.

import tkinter as tk

def on_button_click():
    name = entry.get()
    label.config(text=f"Привет, {name}!")

root = tk.Tk()
root.title("Приветственное приложение")
root.geometry("300x200")

label = tk.Label(root, text="Введите ваше имя:")
label.pack()

entry = tk.Entry(root)
entry.pack()

button = tk.Button(root, text="Отправить", command=on_button_click)
button.pack()

root.mainloop()

Conclusion

Tkinter is a powerful tool for creating GUIs in Python. It provides many widgets and layout methods, making it flexible and easy to use. I hope this article helped you understand the basic concepts of Tkinter and inspired you to create your own applications.

Thank you for your attention! If you have questions or suggestions, leave them in the comments.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *