Dynamo Python, GUI Interface

Hi all.
image

I’m trying to make steps into learning Python within Dynamo. I’ve realised i’ve probably took a step too far and haven’t learned the basics, but I was wondering if anyone could help me out with an issue I have.
I’m in the process of writing a script for work and it was an added bonus I try add in a GUI interface to help with making it user friendly and more accessible to people who don’t “trust” dynamo…

The script side of things is going fine, however i’m stuck trying to create an interface to do a simple yes/no answer with two different outputs, this would be very beneficial if i could do this atleast once because I can string a few of them together to allow people to make choice on outputs.

So if anyone could help me on this, i’d really appreciate it. I essentially want the Python Script node to have to Outputs which are essentially Yes and No, choosing to delete existing circuits or keep them. Also as you notice in the image above, the delete existing circuit text is being cut off, how would I fix that?

Sorry for the probably very basic questions but I’d really like to learn Python with this script and in the short amount of time i’ve learned a little.

import clr
# import windows form 
clr.AddReference("System.Windows.Forms")
#import system drawing 
clr.AddReference("System.Drawing")
# import system
import System

from System.Windows.Forms import *
from System.Drawing import *

value1 = IN[1]

# Create a Class Form
class CreateWindow(Form):
    def __init__(self): 
    
        # Create the Form
        self.Name = value1
        self.Text = value1
        self.Size = Size(500, 150)        
        self.CenterToScreen()

        self.values = []

        # Create Label for Sheet Name
        labelSheetName = Label(Text = "Delete Existing Circuits?")
        labelSheetName.Parent = self
        labelSheetName.Location = Point(30, 20)
        

        # Create TextBox for Sheet Name
        button = Button()
        button.Parent = self
        button.Text = "Yes"
        button.Location = Point(180,20)
        # Register event
        button.Click += self.ButtonClickedYes
        
        # Create TextBox for Sheet Name
        button = Button()
        button.Parent = self
        button.Text = "No"
        button.Location = Point(300,20)
        

        # Create Button = button
        button = Button()
        button.Parent = self
        button.Text = "Close"
        button.Location = Point(400, 80)
        # Register event
        button.Click += self.ButtonClickedClose
        
    # Create button event
    def ButtonClickedYes(self, sender, args):
        if sender.Click:
            self.values.append(self.textboxSheetName.Text)
            self.Close() 
            
	 # Create button event
    def ButtonClickedClose(self, sender, args):
        if sender.Click:
            self.Close()             

if IN[0]:
	form = CreateWindow()
	Application.Run(form)
	
	OUT = form.values
2 Likes

You should take a look at the Data-Shapes package by @Mostafa_El_Ayoubi. I think you will find what your looking for.

2 Likes

First time ever trying those guis, actually pretty interesting and might do more digging on my own later. I got this template to return a Yes or No as the response:

import clr
# import windows form
clr.AddReference("System.Windows.Forms")
#import system drawing
clr.AddReference("System.Drawing")
# import system
import System

from System.Windows.Forms import *
from System.Drawing import *

# Create a Class Form
def CreateMyForm():
    form1 = Form()
    form1.Text = "Guess what"

    button1 = Button()
    button1.Text = "Yes"
    button1.DialogResult = DialogResult.Yes
    button1.Location = Point(10,10)

    button2 = Button()
    button2.Text = "No"
    button2.DialogResult = DialogResult.No
    leftval = button1.Left + button1.Width + 10
    button2.Location = Point(leftval, button1.Top)

    form1.HelpButton = False
    form1.FormBorderStyle = FormBorderStyle.FixedToolWindow
    form1.MaximizeBox = False
    form1.MinimizeBox = False
    form1.AcceptButton = button1
    form1.CancelButton = button2
    form1.StartPosition = FormStartPosition.CenterScreen

    form1.Controls.Add(button1)
    form1.Controls.Add(button2)
    form1.Size = Size(300,300)
    form1.ShowDialog()

    return form1.DialogResult

if IN[0]:
    newform = CreateMyForm()

OUT = newform

guiwindow
guiresponse

Of course, you will have to tweak it for yours but I think the main thing you were missing was the DialogResult.Yes or .No attached to each button.

This website was where I got everything from: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form?view=netframework-4.7.2

So we used completely different types of GUI based on similar things. The only thing you need to do to fix yours was to change the def ButtonClickedYes(self, sender, args): where you had self.values.append(...) change it to self.values.append("Yes"). To add a “No” option, you need to make a new function.

import clr
# import windows form 
clr.AddReference("System.Windows.Forms")
#import system drawing 
clr.AddReference("System.Drawing")
# import system
import System

from System.Windows.Forms import *
from System.Drawing import *

value1 = "Electrical Circuiting Revit Model"

# Create a Class Form
class CreateWindow(Form):
    def __init__(self): 
    
        # Create the Form
        self.Name = value1
        self.Text = value1
        self.Size = Size(500, 150)        
        self.CenterToScreen()

        self.values = []

        # Create Label for Sheet Name
        labelSheetName = Label(Text = "Delete Existing Circuits?")
        labelSheetName.Parent = self
        labelSheetName.Location = Point(30, 20)
        

        # Create TextBox for Sheet Name
        button = Button()
        button.Parent = self
        button.Text = "Yes"
        button.Location = Point(180,20)
        # Register event
        button.Click += self.ButtonClickedYes
        
        # Create TextBox for Sheet Name
        button = Button()
        button.Parent = self
        button.Text = "No"
        button.Location = Point(300,20)
        button.Click += self.ButtonClickedNo
        

        # Create Button = button
        button = Button()
        button.Parent = self
        button.Text = "Close"
        button.Location = Point(400, 80)
        # Register event
        button.Click += self.ButtonClickedClose
        
    # Create button event
    def ButtonClickedYes(self, sender, args):
        if sender.Click:
            self.values.append("Yes")
            self.Close() 

    def ButtonClickedNo(self, sender, args):
        if sender.Click:
            self.values.append("No")
            self.Close() 
            
	 # Create button event
    def ButtonClickedClose(self, sender, args):
        if sender.Click:
            self.Close()             

if IN[0]:
	form = CreateWindow()
	Application.Run(form)
	
	OUT = form.values
1 Like