Python Assistant Prompts: Dynamic Time Display in PyQt


 

Prompt Used:

Can you write a script which displays a non-modal PyQt window within MicroStation 
showing the current time, updating every second?

 

Python Assistant Generated Code:

from MSPyBentley import *
from MSPyECObjects import *
from MSPyBentleyGeom import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *

import sys
import time
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QFont

class ClockWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("MicroStation Clock")
        self.setGeometry(100, 100, 300, 100)
        self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)

        # Create and configure the label to display the time
        self.label = QLabel(self)
        self.label.setAlignment(Qt.AlignCenter)
        self.label.setFont(QFont("Arial", 36))
        self.setCentralWidget(self.label)

        # Timer to update the time every second
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_time)
        self.timer.start(1000)

        self.update_time()

    def update_time(self):
        # Update the label with the current time
        self.label.setText(time.strftime("%H:%M:%S"))

def show_clock_window():
    # Only create QApplication if not already created
    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)

    window = ClockWindow()
    window.show()

    # Non-modal: do not block MicroStation, so use a timer 
    # to pump MicroStation's Python main loop
    def pump_mstn():
        PyCadInputQueue.PythonMainLoop()
    timer = QTimer()
    timer.timeout.connect(pump_mstn)
    timer.start(50)

    # If running outside of MicroStation, start the event loop
    if not hasattr(app, 'mstn_event_loop_started'):
        app.mstn_event_loop_started = True
        app.exec_()


def main():
    show_clock_window()


if __name__ == "__main__":
    main()

 

Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs