Logo

dev-resources.site

for different kinds of informations.

How To Create GUI Window Using Python's Tkinter

Published at
12/22/2023
Categories
python
tkinter
gui
tutorial
Author
fahimfba
Categories
4 categories in total
python
open
tkinter
open
gui
open
tutorial
open
Author
8 person written this
fahimfba
open
How To Create GUI Window Using Python's Tkinter

You can create GUI (Graphical User Interface) based applications using Tkinter (Tkinter is a graphical user interface (GUI) library for Python scripts. It's the only framework built into the Python standard library and is included in all standard Python distributions). In this article, I will show you how you can create a very basic simple GUI window in Python just using a few lines of code. I have also created a video for you! 😊

Video Tutorial

This Python script creates a basic graphical user interface (GUI) application using tkinter, a standard GUI toolkit in Python.

Code



import tkinter as tk
import tkinter.font as tkFont

class App:
def init(self, root):
# Setting the title
root.title('My First GUI Window')
# Setting the window size
height = 720
width = 1280
screenheight = root.winfo_screenheight()
screenwidth = root.winfo_screenwidth()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
root.geometry(alignstr)
root.resizable(width = False, height = False)

if name == "main":
root = tk.Tk()
app = App(root)
root.mainloop()

Enter fullscreen mode Exit fullscreen mode




Output

GUI Window

Let's break down the script:

Import Statements

  1. import tkinter as tk: Imports the tkinter module and gives it an alias tk. This module provides classes and methods to create GUI elements.
  2. import tkinter.font as tkFont: Imports the font submodule from tkinter. It's used to deal with fonts, though it's not explicitly used in this script.

Class Definition: App

  • class App:: Defines a new class App, which will contain the components of the GUI.

Constructor: __init__

  • def __init__(self, root):: The constructor of the App class. It initializes a new instance of the class. root is the main window of the application, typically an instance of Tk.

Inside the Constructor

  1. Setting Title: root.title("undefined"): Sets the title of the window to "undefined".

  2. Setting Window Size:

    • width=600 and height=500: Sets the width and height of the window in pixels.
    • screenwidth = root.winfo_screenwidth() and screenheight = root.winfo_screenheight(): Retrieves the width and height of the screen.
    • alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2): Creates a string to specify the size and position of the window. It centers the window on the screen.
    • root.geometry(alignstr): Applies the size and position settings to the window.
    • root.resizable(width=False, height=False): Disables resizing of the window.

Main Block

  • if __name__ == "__main__":: Checks if the script is being run directly (not imported as a module).
    • root = tk.Tk(): Creates the main window (root) of the application.
    • app = App(root): Instantiates the App class with root as the argument.
    • root.mainloop(): Starts the main event loop of the program. This keeps the window open and waits for user interaction.

Limitations and Further Development

  • The script only sets up a window without any widgets (like buttons, labels, or text fields).
  • To make it a functional application, you would need to add widgets inside the App class.
  • The script also does not handle any events or user interactions.

This is a foundational script for a tkinter application, providing a window setup, but it lacks the interactive components typical of GUI applications.

I will try to make more videos and articles on Python Tkinter-based projects later.

gui Article's
30 articles in total
Favicon
Criando interface grΓ‘fica Desktop nativa utilizando C++ com GTK3
Favicon
Interview with Eson (Seven), Creator of DocKit!
Favicon
Tkinter: Python's Secret Weapon for Stunning GUIs
Favicon
Creating a Native Desktop GUI Using C++ with GTK
Favicon
FEATool Multiphysics 1.17 - Multi-CFD solver and OpenFOAM Simulation Toolbox
Favicon
Introduction to Linux GUIs: Unpacking the Basics of Desktop Environments, Window Managers, and More
Favicon
Gui version for my docker-like solution native to macOS
Favicon
How to Build an SQLite GUI (Fast & Easy Tutorial)
Favicon
Go and WebUI
Favicon
AI Image and AI Chat pocket tool | Python GUI for windows.
Favicon
From Blue To Cool: We’ve given the MIDAS UI a makeover
Favicon
My opinion on the Tauri framework
Favicon
How One Experienced Software Engineer Learns a New Programming Language
Favicon
A detailed comparison of Toad for Oracle and dbForge Studio for Oracle
Favicon
Best alternative to SQLyog
Favicon
Docker - a terminal GUI
Favicon
RSGL | Modular header-only cross-platform GUI Library for easily creating GUI software your way!
Favicon
Rust has Reignited My Love for Programming
Favicon
Code-Along: How to Develop a REST API Dashboard
Favicon
How to Create a SQL Server GUI in 4 Steps
Favicon
Experimenting with GUIs on the Pi Zero
Favicon
Mastering Qt: Complete C++ GUI Course
Favicon
Mastering Qt: Complete C++ GUI Course
Favicon
How to Create a SQL GUI in 4 Steps
Favicon
Creating a GUI in Python With TtkBootstrap
Favicon
Trying egui: building a Cistercian Clock with Rust GUI
Favicon
Building a Custom Alarm Clock with Python and Tkinter
Favicon
How To Create GUI Window Using Python's Tkinter
Favicon
Select the best GUI toolkit – part 7: pyQt
Favicon
Why won't my code open the Tkinter window?

Featured ones: