17. MicroStation Python:创建用户界面


欢迎来到MicroStation Python世界! 本文将特别关注使用TKINTER库制作用户界面(UI)。这种强大的组合允许您在MicroStation环境中创建自定义对话框和工具,从而提高生产力和用户体验。

在我们深入代码之前,有必要了解TKINTER是一个用于创建图形用户界面的标准Python库。它相对易于学习和使用,使其成为快速UI开发的热门选择。

请参阅Python管理器文章以创建和加载Python项目。将项目命名为DialogDemo.py并将其保存到任一文件夹下。在编辑器中打开项目以编写Python脚本。

理解代码

让我们一步一步地分解Python代码:

导入模块

该脚本首先导入几个模块,用于与MicroStation交互并创建TKINTER UI:

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

import ctypes
from tkinter import *
from tkinter import colorchooser

定义函数

创建界面(UI)

main函数构建TKINTER UI:

 关键点

下面是完整的脚本:

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

import ctypes
from tkinter import *
from tkinter import colorchooser


root = Tk()


# Message box function
def MsgBox (title, text, style):
    return ctypes.windll.user32.MessageBoxW (0, text, title, style)


# RGB to String function
def rgb_to_string(rgb_tuple):
    return ', '.join(map(str, rgb_tuple))


# Hex to RGB function
def hex_to_rgb(hex_color):
    # Remove the hash symbol if present
    hex_color = hex_color.lstrip('#')
    
    # Convert hex to RGB
    rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
    return rgb


# Get Level Name List
def GetLevelList ():
    level_List = []

    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    levelCache = ACTIVEMODEL.GetDgnFile().GetLevelCache()

    it = iter(levelCache)
    for level in it:
        levelName = level.GetName()
        level_List.append(levelName)

    return level_List


# Place Line Button
def PlaceLine():
    global my_color
    global selected_level

    if my_color is None:
        my_color = "#646464"

    rgb_color = hex_to_rgb(my_color)
    rgb_str = rgb_to_string (rgb_color)

    level_name = str(selected_level.get())

    keyin_str = "ACTIVE COLOR " + rgb_str + "; ACTIVE LEVEL " + level_name
    keyin_str = keyin_str + "; ACTIVE WEIGHT 2; PLACE LSTRING"
    #print(keyin_str)
    
    PyCadInputQueue.SendKeyin(keyin_str)
    
    root.destroy()


# Get color function
def GetColor():
    global my_color
    global color_label1

    my_color = colorchooser.askcolor()[1]
    my_color = my_color.upper()
    color_label1.config(text=my_color)
    color_label1.config(bg=my_color)


# Main function
def main():
    global my_color
    global color_label1
    global selected_level

    #root = Tk()
    root.title("Dialog Demo...")

    # Color
    my_color = "#646464"

    # Create the widgets
    color_label = Label(root, text="Color: ")
    color_label1 = Label(root, text=my_color, bg=my_color)
    color_button = Button(root, text="Pick A Color", command=GetColor)

    # Use grid to place the widgets
    color_label.grid(row=0, column=0, padx=10, pady=10, sticky="ew")
    color_label1.grid(row=0, column=1, padx=10, pady=10, sticky="ew")
    color_button.grid(row=0, column=2, padx=10, pady=10, sticky="ew")

    # Level
    levelList = GetLevelList ()

    level_label = Label(root, text="Level: ")
    selected_level = StringVar()
    selected_level.set(levelList[0])
    level_option = OptionMenu(root, selected_level, *levelList)

    # Use grid to place the widgets
    level_label.grid(row=1, column=0, padx=10, pady=10, sticky="ew")
    level_option.grid(row=1, column=1, padx=10, pady=10, sticky="ew")

    place_button = Button(root, text="Place Line", command=PlaceLine)

    # Use grid to place the widgets
    place_button.grid(row=2, column=1, padx=10, pady=10, sticky="ew")

    # Configure column weights to spread out evenly
    root.grid_columnconfigure(0, weight=1)
    root.grid_columnconfigure(1, weight=1)
    root.grid_columnconfigure(2, weight=1)    

    root.mainloop()


if __name__ == "__main__":
    main()

运行/执行项目

从Python管理器对话框中选择项目DialogDemo.py并运行/执行Python脚本。

以下UI加载后允许用户使用选定的颜色、层属性创建线串元素。

A screenshot of a computer programDescription automatically generated

探索其它TKINTER小部件和布局选项,通过MicroStation集成创建更复杂、更具视觉吸引力的UI。

祝您编程愉快!