Tag Archives: PyQT

Loading UI Files in Qt for Python

Qt for Python (i.e. PySide2) was announced recently and got me interested in trying to use it to load up a UI file. If you don’t know, PyQt and PySide / PySide2 can use the Qt Creator application to create user interfaces using a drag-and-drop interface. This is actually very similar to the way you would create an interface using Visual Studio. Qt Creator / Designer will generate an XML file with a *.ui extension that you can then load inside of your PySide2 application (or PyQt).


Creating the User Interface

For this example, I opened up Qt Creator and went to File –> “New File or Project”. Then I chose the “Qt Widgets Application” choice. See screenshot below:

Then I opened up the mainwindow.ui that Qt Creator made for me. You can just double-click it or click on the Design button that should be on the left-hand side of the program. Here’s a screenshot that might help:

I added three widgets to my UI:

  • QLabel
  • QLineEdit
  • QPushButton

Continue reading Loading UI Files in Qt for Python

Getting Started with Qt for Python

The Qt Team recently posted that Qt will now be officially supporting the PySide2 project, which they are calling “Qt for Python”. It will be a complete port of the original PySide, which only supported Qt 4. PySide2 supports Qt 5. Qt for Python will have the following license types: GPL, LGPL and commercial.

PySide2 supports Python 2.7 as well as Python 3.4 – 3.6. There are snapshot wheel builds available here.Let’s say we downloaded the Windows Python wheel. To install it, you can use pip like this:

python -m pip install PySide2-5.11.0a1-5.11.0-cp36-cp36m-win_amd64.whl

Once you have PySide2 installed, we can get started by looking at a really simple example:

import sys
from PySide2.QtWidgets import QApplication, QLabel
 
if __name__ == '__main__':
    app = QApplication([])
    label = QLabel("Qt for Python!")
    label.show()
    sys.exit(app.exec_())

This code will create our application object (QApplication) and a QLabel to go on it. When you run app.exec_(), you start PySide2’s event loop. Since we do not specify a size for the label or the application, the size of the application defaults to be just large enough to fit the label on-screen:

Continue reading Getting Started with Qt for Python

PySide: Standard Dialogs and Message Boxes

The PySide GUI toolkit for Python has several standard dialogs and message boxes that you can use as-is. You can also create custom dialogs, but we’ll be saving that for a future article. In this post, we will cover the following dialogs:

  • Color Dialog
  • File Dialog
  • Font Dialog
  • Input Dialog
  • Print & Print Preview Dialogs

We will also cover PySide’s Message Boxes. All code in this article was tested on Windows 7 Professional with PySide 1.2.2 and Python 2.6.6. Now let’s get to it! Continue reading PySide: Standard Dialogs and Message Boxes

PySide: Connecting Multiple Widgets to the Same Slot

As I learn PyQt and PySide, I am writing some tutorials to help my fellow travelers. Today we’ll be looking at how to connect multiple widgets to the same slot. In other words, we’ll be binding the widgets signals (basically events) to slots (i.e. callables like functions, methods) which are better known as “event handlers”. Yes, I know in PySide land that you don’t call it that. It’s a SLOT, not an event handler. But I digress. Anyway, there are two ways to approach this. One is to use functools and its partial class to pass parameters or to use PySide’s introspection abilities to grab that information from the widget that did the calling. There’s actually at least one other method that is very similar to functools.partial and that is to use the infamous anonymous function utility known as lambda. Let’s take a look at how all these methods can be done! Continue reading PySide: Connecting Multiple Widgets to the Same Slot

PySide: Creating a Currency Converter

I am currently reading through Mark Summerfield’s book on PyQt, Rapid GUI Programming with Python and Qt and thought it would be fun to take some of the example applications in it and convert them to PySide. So I’ll be creating a series of articles where I’ll show the original PyQt examples from the book and then convert them to PySide and probably add something of my own to the code. The book doesn’t really get in Qt GUI coding until chapter 4 where the author creates a fun little currency converter. Come along and enjoy the fun! Continue reading PySide: Creating a Currency Converter

PyCon 2011: PyQt Tutorial Sessions I and II

I attended the Creating GUI Applications in Python using Qt I by Paul Kippes this morning and then I attended half of his second tutorial on PyQt this afternoon. The speaker had lots of materials to give us including fairly extensive examples. I think I learned a lot during the morning session because I had a fair amount of hands-on code editing and using Qt’s Designer, which is a WYSIWYG editor for creating the GUI, kind of like Microsoft’s Visual Studio.

The second session was just a talk with no on-hands coding of any kind. I was not engaged during this tutorial very well and decided I would be better off leaving at break and doing something else. I’ll study the speaker’s notes and code examples later. Look forward to some articles on PyQt and perhaps PySide in the future!

Weekly Python News: 01/28/2011

It’s almost the end of January already and PyCon USA 2011 is just around the corner. In this edition, we cover a wide variety of topics, from Python 2.7 – 3.x. We’ve got sprints, new books, web-related projects and good old recursion articles. Come one, come all! Read the news of the week!

On the PyCon front, you should take note the Early Bird pricing is now over. Why did you wait? Fortunately, they are still selling tickets, so stop stalling and just come! The PyCon blog also mentions that they’re still looking for sponsors, so if you or someone you know has that kind of cashola, point them in that direction.

And now we return you back to your regularly scheduled blog reading…