Python

Hello World using PyQt

Version 2

Q How do I pop a dialog with 'Hello World' using MicroStation Python?

Q How do I specify an icon for the dialog?

A Here's a small Python program that uses the PyQt library. It shows a small dialog …

Python Implementation

The Hello World 2 example borrows from Python articles and examples you can find on the Internet. It adds the MicroStation requirements for a dialog to work harmoniously as a Python app.

Class Inheritance

This example builds on the first Hello World example. It uses class inheritance by providing a main window base class MainWindowBase, which is defined in file la_solutions_qt_widgets.py. That base class performs some actions that are required by any QMainWindow. Those actions include that windows messages are passed to MicroStation, so that the Python application doesn't make MicroStation unresponsive.

The base class sets the main window caption …

class MainWindow(la_widgets.MainWindowBase):
    # Initialize the main window class
    def __init__(self):
        # Call the parent class's constructor to initialize the main window
        super().__init__("MicroStation Python")

It sets the main window's icon, which in this example is a smiley face …

Smiley
	iconPath = la_utilities.getFileFromScriptFolder ('smiley-icon.png')
	self.setWindowIcon(QIcon(iconPath))

Python file la_solutions_utilities.py provides a handful of functions that are generally useful but don't belong in any particular project.

The Python source code of Hello World V2 is available for download. The ZIP package includes several files …

The app file is HelloWorld2.py. It uses a couple of other library files …

Download PyQt-HelloWorld2.zip

Unpack the ZIP file and copy the Python file into a folder that MicroStation knows about.

Python Manager

Use MicroStation's Python Manager to find and execute the script.

Questions

Post questions about MicroStation Python programming to the MicroStation Programming Forum.