Multiple string input

Hi, im trying to collect 2 strings from the user while using only one pop up menu.

something like forms.ask_for_string
only for two instead of one.
does anybody knows how can it be done?

Thanks

from pyrevit import forms

forms.ask_for_string(
    default='some-tag',
    prompt='Enter new tag name:',
    title='Tag Manager'
)

@IdoRevit

actually i can`t follow you

do you want to get the inputs of 2 Parameters from an element ?

should the strings match by a key ?

When you use forms.ask_for_string() you can only collect one string input at a time.
I want to collect 2 ( key and the value of the parameter in my case) at a time.
Right now the procces from the user end looks like this:
input 1st string → click ok → input 2nd string → click ok → script continue to run…

I want it to be:
input 1st string & 2nd string-> click ok → script continue to run…

1 Like

just use it twice :slight_smile:

@Tomasz

you mean like “concacinate” ?

I’d go for wpf window with two textboxes. It’s fairly easy solution and user friendly.

2 Likes

@IdoRevit, you can use the FlexForm class found within the Revit Python Wrapper (rpw) which comes shipped with pyRevit. You could do something like…

from rpw.ui.forms import FlexForm, TextBox, Button

components = [TextBox('textbox1', Text='default text1'),
              TextBox('textbox2', Text='default text2'),
              Button('OK')]

form = FlexForm('Title', components)
form.show()

Haven’t personally been able to get this to work on my install of pyRevit, but I’ve seen others use it to do exactly what you’re looking for.
rpw

2 Likes

WPF is the way to go, but Windows Forms could also work.

Here is a sample for setting up a custom WPF to get you started.

Add this xaml code in a file inside your .pushbutton, name the file “CustomForm.xaml”:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Form Title" Height="180" Width="410">
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="5">Input value 1:</TextBlock>
        <TextBox Name="InputValue1TextBox" Grid.Row="0" Grid.Column="1" Margin="5"/>

        <TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" Margin="5">Input value 2:</TextBlock>
        <TextBox Name="InputValue2TextBox" Grid.Row="1" Grid.Column="1" Margin="5"/>

        <Button Name="OkButton" Grid.Row="3" Grid.Column="1" Width="170" Margin="5" HorizontalAlignment="Right" Click="OkButton_Click">OK</Button>
        <Button Name="CancelButton" Grid.Row="3" Grid.Column="0" Width="170" Margin="5" HorizontalAlignment="Right" Click="CancelButton_Click">Cancel</Button>
    </Grid>
</Window>

Script to open the form, fetch the input and print it:

import os
import wpf
from System.Windows import Window

from pyrevit import revit

# Create a class for the custom form
class CustomForm(Window):
    def __init__(self):
        xaml_path = os.path.join(os.path.dirname(__file__), 'CustomForm.xaml')
        wpf.LoadComponent(self, xaml_path)

    # Handler for the OK button
    def OkButton_Click(self, sender, e):
        input_value_1 = self.InputValue1TextBox.Text
        input_value_2 = self.InputValue2TextBox.Text
        
        # Do something with the inputs
        self.DialogResult = True  # Set the dialog result as true to close the window
        self.Close()

    # Handler for the Cancel button
    def CancelButton_Click(self, sender, e):
        self.DialogResult = False  # Set the dialog result as false to indicate cancellation
        self.Close()

# Main pushbutton logic
def main():
    custom_form = CustomForm()

    # Show the form and capture the dialog result
    if custom_form.ShowDialog():
        input_value_1 = custom_form.InputValue1TextBox.Text
        input_value_2 = custom_form.InputValue2TextBox.Text

        # Process the input values here
        # For example, print them
        print("Input Value 1:", input_value_1)
        print("Input Value 2:", input_value_2)

with revit.Transaction('Use Custom Form'):
    main()

With this you can use the “input_value_1” and “input_value_2” as variables for the rest of your script.

3 Likes

Thank you guys for your answers.
both solutions from @pete.heibel and @Jonn.O works.

The solution from Pete is more simple and fits my case well, although Ill keep Jonn’s solution in my mind, it might be helpfull in other cases.

1 Like