DEV Community

Cover image for How To Create GUI Window Using Python's Tkinter
Md. Fahim Bin Amin
Md. Fahim Bin Amin

Posted on

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.


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.resizable(width = False, height = False)

if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
Enter fullscreen mode Exit fullscreen mode


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.

Top comments (0)