Skip to Content
Course content

389: A Look at Alternative GUI Toolkits: PyQt and Kivy

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I worked with a developer named Marc who had built a fantastic internal data-scraping tool using Tkinter. It worked perfectly, but when he demoed it to the executive team, the feedback was brutal: "It looks like it was written for Windows 95." Marc spent the next two weeks trying to "skin" Tkinter, fighting with frames and padding to make it look modern, only to realize he was fighting the tool itself. He eventually scrapped the UI layer and moved to PyQt. Within three days, he had a professional, tabbed interface with a sophisticated docking system that made the app look like a commercial product. That's the moment Marc realized that while Tkinter is great for quick scripts, some projects require a toolkit designed for professional software engineering.

The Industrial Strength of PyQt

When you move into PyQt (or PySide), you're not just using a Python library; you're using a Python wrapper around the Qt framework, which is written in C++. This is why PyQt feels so much more "solid" than Tkinter. It provides a massive array of pre-built widgets—complex tables, tree views, and advanced dialogs—that would take you weeks to build from scratch.

The core concept you need to wrap your head around here is Signals and Slots. In Tkinter, you usually pass a callback function to a button. In PyQt, everything is an event-driven signal. When a user clicks a button, it emits a signal; you "connect" that signal to a "slot" (which is just a function). This decoupling makes it much easier to manage complex applications where one action—like clicking a "Refresh" button—might need to trigger updates in five different parts of the UI simultaneously.

from PyQt6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget

def handle_click():
    print("Data synchronized successfully!")

app = QApplication([])
window = QWidget()
layout = QVBoxLayout()

btn = QPushButton("Sync Database")
# This is the 'Signal' (clicked) connected to the 'Slot' (handle_click)
btn.clicked.connect(handle_click)

layout.addWidget(btn)
window.setLayout(layout)
window.show()
app.exec()

Going Mobile and Multitouch with Kivy

Now, if your goal isn't a "heavy" desktop application but something that needs to run on a tablet or a smartphone, PyQt isn't your best bet. That's where Kivy comes in. Kivy is fundamentally different because it doesn't use the native UI controls of the operating system. Instead, it uses OpenGL to draw its own widgets. This means your app looks exactly the same on macOS, Windows, Linux, Android, and iOS.

The most interesting part of Kivy is the KV Language. I'm a big fan of this approach because it allows you to separate your presentation logic from your Python business logic. Instead of nesting dozens of Python objects to build a layout, you write a .kv file that describes the UI declaratively—almost like CSS for Python. It keeps your Python classes clean and focused on what the app does, rather than how it looks.

Keep in mind, though, that because Kivy draws its own buttons and sliders, it doesn't "feel" like a native Android or iOS app. It feels like a custom app. If you need that native "iOS look," Kivy might feel a bit alien, but for custom kiosks or experimental touch interfaces, it's practically unbeatable.




📋 Practical Task

Build a Kivy-Based Touch Control Panel

Your task is to create a simple touch-interface prototype for a smart-home lighting system using Kivy. Instead of using standard Python layout calls, you must implement the UI using a Kivy string (which simulates a .kv file).

Requirements:

  • Create a Screen containing a BoxLayout.
  • Add two large buttons: one labeled "Living Room Lights" and one labeled "Kitchen Lights".
  • Each button should change the background color of the screen when pressed (e.g., Living Room turns the screen light yellow, Kitchen turns it light blue).
  • Use a Kivy StringProperty or a simple method within the App class to handle the color transition.

Hint: You'll need to use Builder.load_string() to integrate your KV layout with your Python logic.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.