Where to start WPF with python.?

Hello Guys,

I wish to create UI for my Scripts,I have a manageable fluency with python,
I was told that We can create UI with Visual Studio Code and blend I hardly see anything on web related to Python all of that stuffs are in C#
Could you please direct me to right place where I can start learning these concepts.
Also is it really possible that we design UI with the VS blend tool that looks like photoshop (:stuck_out_tongue: atleast to me) and simply extract Iron python codes out for our dynamo Scripts ??

Depending on what you are after doing the DataShapes package on the package manager website by @Mostafa_El_Ayoubi might be able to do what you want to do.

Some information about the package can be found here: https://data-shapes.io/blog/

1 Like

DataShapes uses WinForms, but trying to use WPF in python without any decent debugging options and no view display will be a nightmare even for an experienced developer (they just wouldn’t do it tbh) and it will require far more effort than learning some C# and using Visual Studio.

Nevertheless, if you do want to hazard it:

1 Like

Sorry for the delayed response guys…!
I have been trying to achieve the same with visual studio WPF iron python template and actually where Iam struck is creating a decent script behind the buttons of the WPF form and importing them to Revit via RPS or Dynamo(finding and importing libraries required)

So,If you you have done a fairly simple form using Iron python and could share it it might be helpful.
Directions for learning WPF and VS are also appreciated.

So link that @Thomas_Mahon provided is a pretty good start. If I had to make a recommendation on how to get around the fact that you don’t have any decent options for previewing WPF when creating it via Python in a text editor, maybe don’t do that. You can create the layout in Visual Studio taking advantage of all the great design features there, then simply take the XAML file, and import it into your python code. You will need to write all of the logic - aka code behind - in Python though. I recommend have a look at pyRevit and how its done there. It’s pretty obvious, and a great place to start. Just dig around pyRevit files.

2 Likes

Totally agree with you @Konrad_K_Sobon In fact I came to know about WPF through pyrevit only it is an awesome tool but,I am having difficulties in understanding the xaml it self and most tutorials available use c# which I am not familiar with,have you come across any video that explain how to code a xaml with python?

1 Like

I think you are confusing couple of things here. So the XAML part is generating the UI controls and C# is the code language used to generate the code when said controls are asked to execute something. The two are not connected beyond the fact that most tutorials would show that XAML code is generated using Visual Studio and code behind is written in C#. You can write the XAML in Visual Studio and code behind in Python which is what we are telling you to look at in pyRevit. To make writing XAML code easier, you can use Visual Studio because it has a visual editor that allows you to drag and drop controls and VS generates the XAML code. That’s not a requirement. If you are so inclined you can write XAML manually in a text editor.

3 Likes

@Konrad_K_Sobon ,Sorry I should have actually phrased it better.,
Actually I knew that xaml and python are essentially different things but what I wanted to learn is establishing a connection between xaml buttons and other elements with our RPS python code,In other words xaml itself ( for instance I am able to create a ok button in xaml but wonder how to link it to my script so that it takes the input from the xaml form).
Hope this helps you to direct me in the right direction and thanks for your reply.

Again, have a look at how its done in pyRevit.

  1. Write XAML.
  2. Write python class. This is where code behind exists:

pyRevit has utilities that allow you to quickly reference XAML form into a wrapper class that Ehsan wrote. It makes things easier. But if you want to understand that whole thing from scratch, just follow it back to source. So in the above class there is a utility called forms loaded in from pyRevit.

That seems to be calling another utility from a pyrevit.framework called wpf. This seems to be just place where all of the .NET references get loaded in.

Either way, there is a ton of code written around WPF for pyRevit. Just look at it. You will find everything that you need right there.

3 Likes

As a first step I tried to create a small form with one button that closes the form
and I am getting some error hopefully from xmal end but ,works fine inside Visual studio.
I have created a new post here Xaml error..?

I know this is an old question but if anyone wants to quickly put together a WPF window using Python and doesn’t want to go through any Xaml hassle here is an example:

import clr

clr.AddReference('PresentationFramework')
clr.AddReference('PresentationCore')

from System.Windows import Window
from System.Windows.Controls import StackPanel

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import PreviewControl

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

win = Window()
st = StackPanel()
st.Children.Add(PreviewControl(doc, doc.ActiveView.Id))

win.Content = st
win.Show()

This is obviously very hard to debug so I wouldn’t advise building anything user-facing with this.

3 Likes