• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Python GUI full course — Learn GUI development in 15 mins

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
So, you want to start learning GUI development in Python so you can actually start seeing your applications come to life — not just lines of code in a terminal, but real windows, buttons, inputs, and interactions.

Whether you’re building a desktop tool, a dashboard, or just exploring for fun, GUI development opens up a world of visual, interactive programming.

In this guide, I’ll teach you everything you need to know to build Python GUI, in addition, I’ll be introducing you to a tool called

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

that would make 10x easier for you to build python GUIs


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Choosing a GUI library


There are quite a number of GUI libraries out there, some of the common once among them includes, Tkinter, Kivy, PySide.

If you are just getting started with python GUI development its highly recommended you start with Tkinter as its much more beginner friendly.

In this tutorial, we’ll be using Tkinter to explain the GUI development process.

Everything about widgets in python GUI


In GUI (Graphical User Interface) development, widgets are the building blocks of your application’s user interface. A widget can be anything that the user sees or interacts with on the screen — like:

  • Buttons
  • Labels
  • Text inputs
  • Checkboxes
  • Dropdowns
  • Sliders and more.

Think of them like LEGO bricks. You put them together to build a functional interfaces.

Layout’s in python GUI


In GUI development, layouts control where and how your widgets appear on the screen. In Python’s most popular GUI toolkit, Tkinter, layouts are handled using geometry managers — special methods that place and organize widgets inside windows.

In Tkinter there are 3 layout managers:

  1. pack() — Stacks widgets vertically or horizontally
  2. grid() — Arranges widgets in a table-like grid (rows/columns)
  3. place() — Places widgets at exact x, y coordinates (manual layout) If you want your app to be responsive, (adjust widget sizes according to window size), you’ll either go with pack or grid.
Root Widget in Tkinter


If you’re starting to learn how to make apps with Tkinter, the most popular GUI (Graphical User Interface) library in Python, you’ll always come across two things:


root = Tk()
root.mainloop()

But what do these actually mean? Let’s break it down in plain English.

When you make a GUI app, you need a window to hold all the buttons, text boxes, and labels. That’s what the root window is.

In Tkinter, you create this main window by writing:


from tkinter import *

root = Tk()
root.mainloop()

There can only be one root window, if you want more window’s you’ll be using something called Toplevel we’ll come to this at a later time.

Mainloop in tkinter


mainloop() — also known as the event loop — is the part of a Tkinter program that keeps the app running.

Once you’ve set up your window and added all your widgets (like buttons, labels, etc.), you call mainloop() at the end of your script:


root.mainloop()

This tells Python to keep the window open and to listen for user actions — like clicking a button, typing in a box, or closing the window. Without mainloop(), your window would appear and close immediately.

Essentialy its an infinite while loop, with something like this under the hood.


window_open = True

while window_open:
event_queue()
render_window()

In short, mainloop() is what makes your GUI app interactive and alive.

Now that you understood the basics of Tkinter, we’ll go into more detail in the next chapter

Getting started with tkinter


Let’s start with a small app. And as said before you’ll need a root window and main loop


import tkinter as tk

root = tk.Tk()
root.mainloop()

Now write the rest of the code between these two lines. Anything after Mainloop will not be visible on the screen.

Let start by adding labels using place.

To add a label we’ll call the Label class that takes the parent as the parameter, in our case the parent is the root. The parents can be anything such as Frames, Tk(), Toplevel() etc, on which your items should be placed.


import tkinter as tk

root = tk.Tk()

# Create a label with text
label = tk.Label(root, text="Hello, Tkinter!")
label.place(x=20, y=20)

root.mainloop()

Now this will place the label at 20, 20 position relative to the root window from the top left corner.

Understanding Pack Layout manager in tkinter


The pack() method arranges widgets relative to each other in the parent window. It places widgets in a block-like fashion — either top-to-bottom (default) or side-by-side, depending on the options you provide.

Lets see an example


root = tk.Tk()

label1 = tk.Label(root, text="Top Label", bg="lightblue")
label1.pack()

label2 = tk.Label(root, text="Bottom Label", bg="lightgreen")
label2.pack()

root.mainloop()

In this example, label1 appears on top of label2. This is the default behavior — vertical stacking from the top.

Key Options

  • side – Controls the side to pack against: TOP (default), BOTTOM, LEFT, or RIGHT.
  • fill – Determines how the widget expands: X, Y, or BOTH.
  • expand – When True, lets the widget expand to fill extra space.
  • padx and pady – Adds horizontal and vertical padding around the widget.
Using Pack layout with

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.




Step 1: Click on the main window and change the layout to Flex as shown below


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Step 2: Add two labels from the left widgets tab

Step 3: Click on the label and open the Pack Manager and explore the options.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Yep that’s it! its just as simple as clicking few buttons.

Understanding Grid Layout manager in Tkinter


When your GUI layout needs more structure, especially in rows and columns, Tkinter’s grid layout manager is your best friend. Unlike the simpler pack() method, grid() gives you precise control over widget placement.

What is the Grid Manager?


The grid() method arranges widgets in a table-like structure using rows and columns. You specify where each widget should go using the row and column options.


import tkinter as tk

root = tk.Tk()

tk.Label(root, text="Username:").grid(row=0, column=0)
tk.Entry(root).grid(row=0, column=1)

tk.Label(root, text="Password:").grid(row=1, column=0)
tk.Entry(root, show="*").grid(row=1, column=1)

tk.Button(root, text="Login").grid(row=2, column=0, columnspan=2)

root.mainloop()

When to Use Grid
Ideal for form layouts, data entry screens, and any structured design.

Allows fine-tuned positioning and spacing.
Cannot be mixed with pack() in the same parent widget.

Using Grid layout with PyUiBuilder


Step 1: Click on the main window and change the layout to Grid as shown below.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Step 2: Decide on the number of rows and columns from the grid configure


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Step 3: Add label and entry widget by dragging and dropping

Step 4: Adjust rows and column by clicking on the widget and adjusting from grid manager


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Oh! and don’t forget to export your code from top right corner.

That was it, now you can try out other widgets on

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

and familiarize yourself with Tkinter.



Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

, for help with tkinter or python GUI development


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу