Notes of creating winform GUI in Dynamo Revit using Python [0]
As the online resources about this topic is getting less and less, I would like to share what I have collected and learned incase of all the links and tutorial are vanished in the internet.
At the time of making this notes, I am using Revit 2023 and Dynamo Revit 2.16.1.6510. Winform API run smoothly on Ironpython2 engine, so don't need to be worry if you are using older versions. But I would like to test the compatibility on Cpython3 engine.
First, we need to create a python script node in Dynamo.
Inside the node, we can use the following code to create a simple form.
import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import *
class MainForm(Form):
def __init__(self):
self.Text = "Topic"
self.Width = 400
self.Height = 400
self.MaximizeBox = True
self.MinimizeBox = True
self.ControlBox = True
self.Opacity = 1
self.SizeGripStyle = SizeGripStyle.Show
self.FormBorderStyle = FormBorderStyle.FixedSingle
self.StartPosition = FormStartPosition.CenterScreen
self.TopMost = True
Application.Run(MainForm())
Expected result will be like this
Now, lets explain the code line by line
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import *
This is for importing necessary module.
For Ironpython2.7, clr is required to be imported and add the reference of "System.Windows.Forms"
class MainForm(Form):
Setup a new Form class (object)
def __init__(self):
define the first function will be run in the Form
self.Text = "Topic"
it is for changing the name of the topic on top.
self.Width = 400
self.Height = 400
This is for defining the size of the form and there are another way to define it with Size class imported from System.Drawing.
clr.AddReference('System.Drawing')
from System.Drawing import Size
class MainForm(Form):
def __init__(self):
self.Text = "Topic"
self.Size = Size(200,600)
It has the same effect as defining the size separately. the variables in the Size class are Size(Width,Height).
领英推荐
self.MaximizeBox = True
self.MinimizeBox = True
self.ControlBox = True
These 3 lines representing the buttons on top right corner.
Replacing True with False can take out the button from the Form.
self.Opacity = 1
As the name suggested, it controls the Opacity of the form
self.SizeGripStyle = SizeGripStyle.Show #to show the indictor
self.SizeGripStyle = SizeGripStyle.Hide #to hide the indictor
It is for controlling the visibility of the sizable indictor at the bottom right corner.
self.FormBorderStyle = FormBorderStyle.FixedSingle
This line is to set the Form Style. Some styles will override the setting above.
My personal favourite is FixedSingle, but there are 6 more options in the following API document.
self.StartPosition = FormStartPosition.CenterScreen
After setting up the style of the form, we need to decide where it should be started. There are 5 options in total and they can be found in the following link.
self.TopMost = True
Another setting I would normally set the TopMost parameter as True to make sure it is on top of all other programs.
Application.Run(MainForm()) #Non-Blocking
MainForm().ShowDialog() #Blocking
The last line is to kick off the form with the setting we defined.
But there is 2 ways to do it, one is Non-blocking, another one is blocking.
I like to use the non-blocking method because allows me to create function that need to interact with Revit.
I know this is an old topic and now there are more fancy way to do GUI. But I am currently getting sick so I want to do something easier.