Message 1 of 2
Custom XAML UI Window
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hey all, I am writing a script that has many user inputs, and would like to use a custom UI window for the purposes of collecting many user inputs at once. I have virtually no experience with XAML files and would appreciate help for getting started. I am currently using pyRevit for coding in Revit. Atached is barebones code that opens a window, but I am unable to interact with the submit button (ChatGPT code so may be incorrect).
from pyrevit import forms
# Define the XAML layout with text boxes for variable input
layout = '<Window ' \
'xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ' \
'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ' \
'ShowInTaskbar="False" ResizeMode="NoResize" ' \
'WindowStartupLocation="CenterScreen" ' \
'HorizontalContentAlignment="Center">' \
'<StackPanel Margin="10">' \
' <StackPanel Orientation="Horizontal" Margin="0,0,0,10">' \
' <TextBlock Text="Variable 1: "/>' \
' <TextBox x:Name="var1" Width="100"/>' \
' </StackPanel>' \
' <StackPanel Orientation="Horizontal">' \
' <TextBlock Text="Variable 2: "/>' \
' <TextBox x:Name="var2" Width="100"/>' \
' </StackPanel>' \
' <Button x:Name="submit_button" Content="Submit" Width="100" Height="30"/>' \
'</StackPanel>' \
'</Window>'
# Define the function to handle button click event
def on_submit_button_click(sender, args):
var1_text = wpf_window.GetElementByName('var1').Text
var2_text = wpf_window.GetElementByName('var2').Text
print("Variable 1:", var1_text)
print("Variable 2:", var2_text)
wpf_window.Close()
# Create and display the WPF window
wpf_window = forms.WPFWindow(layout, literal_string=True)
# Assign the event handler to the Click event of the button
try:
wpf_window.GetElementByName('submit_button').Click == wpf_window.GetElementByName('submit_button').Click + forms.WPFEventHandler(on_submit_button_click)
except Exception as ex:
print("Exception:", ex)
wpf_window.show()