Using Message Boxes Python

Forums: 

This is the python code to go along with the Tutorial User interaction using message boxes.

In the code samples below, I will show how to create message boxes in a few different methods.
The first sample will be using the built in NXOpen message box.

# NX 11.0.2.7
# Journal created by UGPerson for use on
#Python Version 3.4

import NXOpen
import NXOpen.UF

def main() :
theSession = NXOpen.Session.GetSession()#: :type theSession: NXOpen.Session
workPart = theSession.Parts.Work#: :type workPart: NXOpen.BasePart
displayPart = theSession.Parts.Display#: :type displayPart: NXOpen.BasePart
theUFSession = NXOpen.UF.UFSession.GetUFSession()#: :type theUFSession: NXOpen.UF

theUI = NXOpen.UI.GetUI()#: :type theUI: NXOpen.UI
MsgBox = theUI.NXMessageBox#: :type theMessageBox: NXOpen.UI.theUI.NXMessageBox
MsgBox.Show("Hello Info", NXOpen.NXMessageBox.DialogType.Information, "Hello World")
if __name__ == '__main__':
main()

Before we go any further the IDE that I am using is Eclipse Portable with Pydev, so some of the formatting might not be need for your environment. With that said you might not need the comments at the end of some of the lines. In Eclipse this portion “#: :type theSession: NXOpen.Session” is used for type hinting for the objects.

This portion of the code is used to define and display the message box.

theUI = NXOpen.UI.GetUI()#: :type theUI: NXOpen.UI
MsgBox = theUI.NXMessageBox#: :type theMessageBox: NXOpen.UI.theUI.NXMessageBox
MsgBox.Show("Hello Info", NXOpen.NXMessageBox.DialogType.Information, "Hello World")

This is a lot of work to just show a message box. We could define a function and make it a lot easer to call with something like this.

def MsgBox(text, style, title):
"""
:param text: What you want displaed in the message box.
:type text: str
:param style: What type of massage box is this (info err quest warn)
:type style: str
:param title: This is the lable of the message box
:type title: str
:rtype str
"""

theUI = NXOpen.UI.GetUI()#: :type theUI: NXOpen.UI
MsgBoxDef = theUI.NXMessageBox#: :type theMessageBox: NXOpen.UI.theUI.NXMessageBox
info = NXOpen.NXMessageBox.DialogType.Information
err = NXOpen.NXMessageBox.DialogType.Error
quest = NXOpen.NXMessageBox.DialogType.Question
warn = NXOpen.NXMessageBox.DialogType.Warning

if style == 'info':
return MsgBoxDef.Show(title, info, text)

if style == 'err':
return MsgBoxDef.Show(title, err, text)

if style == 'quest':
return MsgBoxDef.Show(title, quest, text)

if style == 'warn':
return MsgBoxDef.Show(title, warn, text)

Now to create a message box you can call it is just like you would in VB.

MsgBox("Hello World!", "info", "Information")

In python you can call a function that is in a different python file. So what I do is create a file with all of the functions that I would like to use in other programs. To do that just create a file called NXDef.py and save it in the same location as the other journals that you want this to be used in.
Code for NXDef.py

import NXOpen

def MsgBox(text, style, title):
"""
:param text: What you want displaed in the message box.
:type text: str
:param style: What type of massage box is this (info err quest warn)
:type style: str
:param title: This is the lable of the message box
:type title: str
:rtype str
"""

theUI = NXOpen.UI.GetUI()#: :type theUI: NXOpen.UI
MsgBoxDef = theUI.NXMessageBox#: :type theMessageBox: NXOpen.UI.theUI.NXMessageBox
info = NXOpen.NXMessageBox.DialogType.Information
err = NXOpen.NXMessageBox.DialogType.Error
quest = NXOpen.NXMessageBox.DialogType.Question
warn = NXOpen.NXMessageBox.DialogType.Warning

if style == 'info':
return MsgBoxDef.Show(title, info, text)

if style == 'err':
return MsgBoxDef.Show(title, err, text)

if style == 'quest':
return MsgBoxDef.Show(title, quest, text)

if style == 'warn':
return MsgBoxDef.Show(title, warn, text)

MsgBox("Hello World!", "info", "Information")

The code that you want to use message box function in would look like this.

# NX 11.0.2.7
# Journal created by UGPerson for use on
#Python Version 3.4

import NXOpen

from NXDef import MsgBox

def main() :

theSession = NXOpen.Session.GetSession()#: :type theSession: NXOpen.Session
workPart = theSession.Parts.Work#: :type workPart: NXOpen.BasePart
displayPart = theSession.Parts.Display#: :type displayPart: NXOpen.BasePart

MsgBox("Hello World!", "info", "Information")
MsgBox("Hello World!", "err", "Error")
MsgBox("Hello World!", "quest", "Question")
MsgBox("Hello World!", "warn", "Warning")

if __name__ == '__main__':
main()

Make sure that both files are saved in the same location and it should work.

If there is interest in this I can post 3 other ways to do this using NX’s Uf, python built in ctypes and Tkinter.

I've not yet tried out Tkinter; I'd love to see an example of that with NXOpen.