Q How do I specify an icon for the dialog?
Q How do I organise the layout of a dialog?
Q How do I show a menu on a dialog?
Q How do I specify an icon for a menu?
A Here's a small Python program that uses the PyQt library. It shows a small, resizable, dialog …
The Hello World 4 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.
This example uses the
PyQt
user interface (abbreviated to UX or UI).
PyQt terms its UX objects widgets.
A widget is part of a window or dialog: a window or dialog usually contains several widgets.
In this example we use QWindow
, QMenu
, QLabel
and other widgets.
This app adds a menu bar to the dialog, by calling QMainWindow.menuBar()
.
The menu is placed automatically at the top of the dialog.
The dialog uses a QGridLayout.
The text in that grid layout is displayed by
QLabel widgets.
The app calls menuBar.addMenu()
, once for the File menu,
twice for the Count menus,
and again for the Help menu.
Beneath the menu bar,
each menu contains items, in this example the File|Exit and Help|Documentation sub-menus.
When you choose a menu item, the PyQt menu triggers an action.
The File|Exit menu triggers the exitCall()
,
which in turn calls QWindow.close()
to exit the app …
exitCall()
, which calls QWindow.close()
to exit the app
count_ellipses()
, which invokes a specialised element counter
count_lines()
, which invokes a specialised element counter
helpDocCall()
, which calls your browser to open a web page
This example builds on the Hello World examples.
It uses class inheritance by providing a main window base class WindowBase
,
which is defined in file qt_window_base_class.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.
class MainWindow(WindowBase): # Initialize the main window class def __init__(self): # Call the parent class's constructor to initialize the main window super().__init__("MicroStation Python") # The Window ID may be useful when interacting with Microsoft Win32 API #self.storedWinId = self.winId()
It sets the main window's icon, which in this example is a smiley face …
iconPath = la_utilities.getFileFromScriptFolder ('smiley-icon.png') self.setWindowIcon(QIcon(iconPath))
The message pump ensures that Windows messages are not blocked. A timer updates MicroStation's UI every so often so that it remains responsive …
# This function was adapted from the delivered example QtClock def start_message_pump_timer(self): _TICK = 1000 # milliseconds timer = QTimer() # create a QTimer object timer.timeout.connect(updateMstn) # connect the timer's timeout signal to the updateMstn function timer.start(_TICK) # start the timer with an interval of _TICK milliseconds
def updateMstn ():
''' Called by the message pump timer. '''
PyCadInputQueue.PythonMainLoop()
There are several Python utility modules …
la_solutions_utilities.py
provides a handful of functions that
are generally useful but don't belong in any particular project
qt_window_base_class.py
provides the window base class
The Python source code of Hello World V4 is available for download. The ZIP package includes several files …
HelloWorld4.py
la_solutions_qt_widgets.py
qt_window_base_class.py
la_solutions_utilities.py
Icon files are in the Icons
folder …
smiley-icon.png
(the Smiley icon shown in the dialog's top-left corner)
icons8-mouse.svg
sigma-svgrepo-com.svg
(the Sigma Σ symbol shown in the Count Elements menu)
exit-svgrepo-com.svg
(the File|Exit icon)
icons8-info-64.svg
(the Help i icon)
The app file is HelloWorld4.py
.
It uses several Python library files …
qt_window_base_class.py
provides the window base class for the main window in the app file.
la_solutions_utilities.py
provides some general-purpose utilities.
Unpack the ZIP file and copy the Python files into a folder that MicroStation knows about. Copy the images files into the same folder.
Use MicroStation's Python Manager to find and execute the script.
Post questions about MicroStation Python programming to the MicroStation Programming Forum.