6 Python GUI Frameworks to Create Desktop, Web, and even Mobile Apps. | 1
|

6 Python GUI Frameworks to Create Desktop, Web, and even Mobile Apps.

You could build beautiful apps purely in Python.

No solution is complete without a great UI.

Python has been successful in a wide variety of use cases. But few understand and appreciate its ability to create graphical user interfaces (GUI.) Indeed we have a handful of extraordinary Python GUI frameworks that helps developers quickly put up an interface to interact with their project.

This post will discuss 3 cross-platform GUI frameworks and 2 web interfacing tools. We’ll also cover some basic examples and their strengths so that you can be well-acquainted with picking the right one for your next project.

Here are the five tools you need to know for building helpful UI.

Grab your aromatic coffee (or tea) and get ready…!

Gradio

Gradio is a beautiful web UI builder. It’s specifically designed for machine learning projects.

Gradio has about 25 different widgets. Each has its own set of configurations. You can wrap each Python function in a Gradio interface and display its output on another widget.

What’s cool about Gradio is its ability to collect feedback. As data scientists, you often need to collect feedback from evaluators on your models.

Gradio has a flag option. If the user finds the model prediction is off. They could click on the flag button. Gradio will collect all the input values, the prediction, and other information.

As most experienced data scientists know, building and deploying models is a highly iterative process. Hence, feedback collection makes Gradio the perfect fit for ML app prototyping.

You could also deploy your app on Huggingface spaces for free. That way, you can share your work with a wider audience and collect more feedback. Huggingface only allows Gradio and Streamlit apps (and static pages) on their platform.

Which makes Streamlit the next best Python tool for GUI development.

Streamlit

Streamlit is a fantastic tool for developing web apps using Python. It has a declarative coding style which I found very helpful.

You don’t have to know any HTML, CSS, or JavaScript or follow any web development online courses. If you know Python, you are already armed with everything you need.

You can install Streamlit with the following command.

pip install streamlit
Bash

Here’s a simple application that displays a pandas data frame on a web page.

import streamlit as st
import pandas as pd

df = pd.read_csv(
  "https://raw.githubusercontent.com/ThuwarakeshM/PracticalML-KMeans-Election/master/voters_demo_sample.csv"
)

st.title("Interactive K-Means Clustering")

st.write("Here is the dataset used in this analysis:") 

st.write(df)
Python

Create a file named quickstart.py with the above content. To start the streamlit web server, run the following command.

streamlilt run quickstart.py
Bash

This command will start a development server, and on your web browser, you can see the following.

 

Basic Streamlit application

You can learn more about Streamlit in my previous post on the topic. There is also a live version of this app you can check out.

Plotly Dash

Plotly has been around for several years. Dash is a framework that extensively leverages Plotly charts to build GUIs with less code.

Plotly Dash is a flexible solution that fits a variety of applications. Their website says it’s designed to reach places where Tableau and PowerBI can not.

In my opinion, it does.

Install Plotly Dash with the following single command from the PyPI repository.

pip install dash
Bash

To start, you can create a file named app.py with the following content and run it like any other Python script.

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd

app = dash.Dash()

df = pd.read_csv(
    "https://raw.githubusercontent.com/ThuwarakeshM/geting-started-with-plottly-dash/main/life_expectancy.csv"
)

fig = px.scatter(
    df,
    x="GDP",
    y="Life expectancy",
    size="Population",
    color="continent",
    hover_name="Country",
    log_x=True,
    size_max=60,
)

app.layout = html.Div([dcc.Graph(id="life-exp-vs-gdp", figure=fig)])

if __name__ == "__main__":
    app.run_server(debug=True)
Python

Run the below code to activate the webserver. It’ll initiate a Flask development server and open it up on your default web browser.

python app.py
Bash

Plotly Dash app example

You can read more about Plotly Dash in my previous blog post on this topic.

Plotly Dash and Streamlit are more mature technologies in this list. While they are very helpful in developing a web application, we have tools that help us build cross-platform apps in Python. The next one is a great example.

Kivy

Kivy is a cross-platform Python software development framework. With it, you can create Desktop applications for Windows, OS X, and Linux, build mobile apps for iOS and Android, and use it on embedded devices on top of Raspberry Pi.

You’ve read it correctly.

Python is no longer limited to web and desktop development. You can do a lot more stuff you can’t do with other languages. Perhaps, that’s why Python is the most popular technology in the StackOverflow developer survey, 2021.

Kivy’s documentation includes detailed guides from installation to publishing in Android Marketplace. They are significantly different from platform to platform.

Since I’m on Ubuntu (Linux), I’m using the Kivy official PPA way to install it. Following are the instructions to do it.

$ sudo add-apt-repository ppa:kivy-team/kivy
$ sudo apt-get install python3-kivy
Bash

Once installed, you can create a file app.py with the following content and run it to see Kivy in action.

from typing import Text
import kivy
from kivy.uix.widget import Widget

kivy.require("2.0.0")  # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout


class HomePage(GridLayout):
    def __init__(self, **kwargs):
        super(HomePage, self).__init__(**kwargs)

        self.cols = 2

        self.add_widget(Label(text="Hello, what's your name?"))
        self.add_widget(TextInput(multiline=False))


class MyApp(App):
    def build(self):
        return HomePage()


if __name__ == "__main__":
    MyApp().run()
Python

 

GUI created with Kivy

 As you can see in the above example, Kivy’s Grid layout is handy for arranging UI elements on the screen. You only have to specify the number of columns and start adding elements. Kivy is smart enough to display them in the best possible way.

Of course, Kivy is more than this. From their official docs, you can learn more about GPU optimization, mobile builds, touch support, etc. Here’s an excellent Freecodecamp tutorial to watch on Youtube.

Kivy is a great tool to prototype your mobile applications. If your team doesn’t have the skillset to develop mobile apps, this may be the best choice.

PyQt5

PyQt5 is a framework built around QT. QT (and PYQT5) supports cross-functional development. It supports Windows, Mac, Linux, and mobile platforms such as iOS and Android.

Besides its impressive UI components, PyQT5 integrates well with native OS APIs. A great benefit of PYQT5 is the QT Designer, a tool that lets you visually compose UI with QT widgets.

You can install the framework using PyPI as follows:

pip install PyQt5
Bash

A hello-world style app would only have a few lines of code that put up a nice-looking UI.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

def window():
   app = QApplication(sys.argv)
   widget = QWidget()

   textLabel = QLabel(widget)
   textLabel.setText("Hello World!")
   textLabel.move(90,85)

   widget.setGeometry(50,50,320,200)
   widget.setWindowTitle("My first PyQt5 project")
   widget.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   window()

sys.exit(app.exec_())
Python

 

PyQT5 application example

QT and PyQT5 are mature frameworks. Because its ecosystem is vast, it might take some time to master it. Yet, it won’t be any difficult to find free learning resources.

PyQT5, however, is not free for commercial use. The next one is.

Tkinter

Tkinter is a standard Python GUI library. Like Kivy and PyQT5, Tkinter too works on Windows, Linux, and Mac OS. Since Tkinter uses OS native elements, they look and feel like everything else on the platform it’s running on.

Tkinter is relatively easy to learn and implement. But its limitation is its lack of advanced widgets. Nevertheless, it’s a great tool to prototype your ideas in a very short time.

Official Python installation comes with Tkinter. Yet, if you are on Ubuntu, you’d have to install it with the following instructions.

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.9 python3-tk
Bash

Once you have installed the package, you can execute the following Python script to spin up a hello world app.

import tkinter as tk

root = tk.Tk()

txt = tk.Label(root, text="My First Tkinter App!")

txt.pack()

root.mainloop()
Python

The above code will open up a native-looking GUI like the one below.

 

Tkinter app example

 As you can see, this one is very straightforward compared to our previous tools.

 Final thoughts

Over the past couple of decades, Python has expanded into multiple territories. Today we use this elegant programming language for data science, workflow integration, software development, etc.

Python is fully capable of GUI development as well. That’s precisely what we’ve covered in this post.

The five tools we discussed here are helpful for almost all GUI development needs. From prototyping to mobile app releases to Google and iOS stores to Web applications. The only skill set you should have for all these tools is Python.

Choosing the right tool for the right project is art. From this list, my suggestion is as follows:

  • Looking for quick prototyping? Go with Tkinter
  • Need more widgets? Go with PyQT5
  • Developing cross-platform and mobile apps? Kivy works well
  • Want to build a web app and give users concurrent access? Streamlit is fantastic
  • Need more flexibility? Plotly Dash is the answer.

I hope this guide is informative in choosing the Python GUI tool for your next project.

 

Did you like what you read? Consider subscribing to my email newsletter because I post more like this frequently.

Thanks for reading, friend! Say Hi to me on LinkedIn, Twitter, and Medium.

Similar Posts