DEV Community

loading...
Cover image for How to Create GUI Applications with Python

How to Create GUI Applications with Python

jones268 profile image jones268 ・2 min read

Python started as primarily a command line language. That means that it is most typically used to write scripts to automate tasks or to perform some sequence of actions.

GUI applications are an integral part of our daily lives. Yet, when most people are learning to program, they spend hours on making a working terminal program in Python.

How to start making GUI applications with Python? 🐍

You can create a GUI with a library. There are several libraries you can use.

PyQt 🚀

PyQt is the most popular Python bindings for Qt, a cross-platform GUI framework. It comes with many widgets and has a modern look and feel. Some things made with it are the KDE desktop and related applications.

PyQt works on all operating systems including Windows, Mac OS X and Linux. It also comes with a drag and drop designer program.

If you are new to PyQt, I recommend this course

pyqt has a modern appearance

Hello World in PyQt looks like this:

import sys

from PyQt5.QtWidgets import QApplication, QWidget

if __name__ == '__main__':
    app = QApplication(sys.argv)

    w = QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.show()

    sys.exit(app.exec_())
Enter fullscreen mode Exit fullscreen mode

source: hello world

tkinter 😄

You can use Tkinter as your GUI library. It is the most commonly used GUI library in Python. If you don't have Tkinter in your environment, you can install it either by adding Python's Tkinter package or by installing it from your distro's repository.

The limitations of tkinter is that it doesn't have many widgets available and it has quite an old fashioned look and feel.

Hello world in tkinter looks like this. To learn more about tkinter see this article

import tkinter as tk
top = tk.Tk()
top.title("Hello World")
labelHello = tk.Label(top, text = "Hello World! ")
labelHello.pack()
top.mainloop()
Enter fullscreen mode Exit fullscreen mode

Tkinter has a traditional look (90s) and feel

tkinter has a traditional look and feel

pysimplegui

Another option is PySimpleGUI. This lets you create very basic user interfaces using a layout. Underneath it uses PyQt, tkinter and remi so it's a good idea to learn those first.

import PySimpleGUI as sg                        # Part 1 - The import

# Define the window's contents
layout = [  [sg.Text("What's your name?")],     # Part 2 - The Layout
            [sg.Input()],
            [sg.Button('Ok')] ]

# Create the window
window = sg.Window('Window Title', layout)      # Part 3 - Window Defintion

# Display and interact with the Window
event, values = window.read()                   # Part 4 - Event loop or Window.read call

# Do something with the information gathered
print('Hello', values[0], "! Thanks for trying PySimpleGUI")

# Finish up by removing from the screen
window.close()     
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app