How to create an Ansys UI Toolkit GUI box to get input from the user?

Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 457
100 Answers 250 Likes 100 Comments Second Anniversary
✭✭✭✭
edited June 2023 in Structures

How to create an Ansys UI Toolkit GUI box to get input from the user?

Tagged:

Best Answer

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 457
    100 Answers 250 Likes 100 Comments Second Anniversary
    ✭✭✭✭
    edited November 2023 Answer ✓

    Note: Ansys UI Toolkit is not supported.

    import clr
    clr.AddReference("Ans.Utilities")
    clr.AddReference('Ans.UI.Toolkit')
    clr.AddReference('Ans.UI.Toolkit.Base')
    import Ansys.UI.Toolkit
    import Ansys.UI.Toolkit.Base
    
    class Example1Window(Ansys.UI.Toolkit.Window):
    
        aLabel = None
        aButton = None
        aTextBox = None
        mainPanel = None
    
        def __init__(self):
            self.Location = Ansys.UI.Toolkit.Drawing.Point(100,100)
            self.Text = 'Example One'
            self.Size = Ansys.UI.Toolkit.Drawing.Size(200,100)
            self.StatusBar.Visible = False
            self.__BuildUI() #this will be defined later
            self.BeforeClose += Ansys.UI.Toolkit.WindowCloseEventDelegate(self.Window_Close)
    
        def __BuildUI(self):
            self.mainPanel = Ansys.UI.Toolkit.TableLayoutPanel()
            self.Add(self.mainPanel)
            self.mainPanel.Columns.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 100)
            self.mainPanel.Rows.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 33)
            self.mainPanel.Rows.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 34)
            self.mainPanel.Rows.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 33)
            self.aLabel = Ansys.UI.Toolkit.Label('This is a Label!')
            self.mainPanel.Controls.Add(self.aLabel, 0, 0)
            self.aTextBox = Ansys.UI.Toolkit.TextBox()
            self.aTextBox.Text = 'A Text Box'
            self.mainPanel.Controls.Add(self.aTextBox, 1, 0)
            self.aButton = Ansys.UI.Toolkit.Button('Here is a Button!')
            self.aButton.Click += Ansys.UI.Toolkit.EventDelegate(self.On_Click)
            self.mainPanel.Controls.Add(self.aButton, 2, 0)
    
        def On_Click(self, sender, args):
            resultString = 'You clicked the button!' + '\n'
            resultString += 'You typed \'' + self.aTextBox.Text + '\'.'
            Ansys.UI.Toolkit.MessageBox.Show(self, resultString, 'Example One', Ansys.UI.Toolkit.MessageBoxType.Info, Ansys.UI.Toolkit.MessageBoxButtons.OK)
            print self.aTextBox.Text
        
        def Window_Close(self, sender, args):
            Ansys.UI.Toolkit.Application.Quit()
    
    class ExampleOne:
    
        ex1 = None
        def __init__(self):
            self.ex1 = Example1Window()
        
        def Run(self):
            Ansys.UI.Toolkit.Application.Initialize()
            self.ex1.Show()
            Ansys.UI.Toolkit.Application.Run()
            
    example = ExampleOne()
    example.Run()
    

Answers

  • Mateusz
    Mateusz Member Posts: 11
    First Anniversary Name Dropper First Comment
    **

    Hi Ayush!
    Thanks for a great example. So far I've been using System.Windows.Forms instead of Ansys.UI.Toolkit. You mention training and Toolkit documentation, but I can't find either. Could you share where to find it? I found a post that stated, there is no such documentation (https://discuss.ansys.com/discussion/2130/is-there-any-documentation-for-the-ansys-ui-toolkit).

    Also, I tried to use ComboBox, but I cannot change its style to DropDown. I get various errors like:
    Traceback (most recent call last):
    AttributeError: attribute 'DropDownStyle' of 'namespace#' object is read-only

    In regular Windows.Forms it works more or less like:
    self._comboBox = System.Windows.Forms.ComboBox()
    self._comboBox.DropDownStyle = ComboBoxStyle.DropDownList
    self._comboBox.Items.Add("a")

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 347
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    I am not 100% sure, but I don’t believe the UI Toolkit is officially supported or documented. That said, I find it quite useful. Windows forms is also quite useful in a similar way.

    For an example of more advanced implementation of the UI toolkit you can refer to the bolt tools add on in mechanical. If you enable this, add on and open the script console, you can go to that extension scope, and get the directory for the add-on. In this location you will find numerous files that use this UI toolkit, but specifically WizardControlsV2.py will have lots of helpful tools for building UI that allows for geometry selection, tree, object, selection, drop down options, text, or numeric input, etc.…. For examples of using that module refer to almost any file that ends in GUI.

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 457
    100 Answers 250 Likes 100 Comments Second Anniversary
    ✭✭✭✭
    edited November 2023

    @Mateusz, sorry for the confusion. As I added in the note on the top of the code, "Ansys.UI.Toolkit" is not officially supported and nor is it documented. I removed the line that caused the confusion.

    Please follow the hints provided by @Mike.Thompson for more examples on UI Toolkit but you can use Winforms as well for GUI functionalities.

  • Mateusz
    Mateusz Member Posts: 11
    First Anniversary Name Dropper First Comment
    **

    Thanks @Mike.Thompson and @Ayush Kumar for such a quick response! I should start to use this forum more often :)

    Think I'll stick to Winforms then. But I'll definitely check the Bolt Tool out, as I find it pretty interesting inside the Mechanical!