Python Windows Form - Code Not Working

Hi All, - I’m following this python forms tutorial on Youtube [https://www.youtube.com/watch?v=J-DIXsT7zug], before I spin off my own one for my project - but the code doesn’t seem to be working? - Well, I think I’ve missed a reference file somewhere?

I’ve attached the dyn but here’s the core code.

Forms.dyn (6.5 KB)

>     # Enable Python support and load DesignScript library
> import clr
> import System
> clr.AddReference('System.Windows.Forms')
> clr.AddReference('System.Drawing')
> from Systems.Windows.Forms import*
> from System.Drawing import*
> 
> # Create a class form
> class CreateWindow(Form):
> 	def _init_(self)
> 
> 		# Create Form
> 		self.Name = 'Create Window'
> 		self.Text = 'Create Window'
> 		self.Size = Size(500, 150)
> 		self.CenterToScreen()
> 	
> 		self.values = []
> 	
> 		#Create label for sheet name
> 		labelSheetName = Label(Text = "Sheet Name"
> 		labelSheetName.Parent = self
> 		labelSheetName.Location = Point(30,20)
> 	
> 		#Create label for Sheet number
> 		labelSheetName = Label(Text = "Sheet Number"
> 		labelSheetName.Parent = self
> 		labelSheetName.Location = Point(30,50)
> 	
> 		#Create textbox for sheet name
> 		self.textboxSheetName = TextBox()
> 		self.textboxSheetName.Parent = self
> 		self.textboxSheetName.Text = "Sheet Name"
> 		self.textboxSheetName.Location = Point(150,20)
> 		self.textboxSheetName.Width = 150
> 	
> 		#Create textbox for sheet number
> 		self.textboxSheetNumber = TextBox()
> 		self.textboxSheetNumber.Parent = self
> 		self.textboxSheetNumber.Text = "Sheet Number"
> 		self.textboxSheetNumber.Location = Point(150,50)
> 		self.textboxSheetNumber.Width = 150
> 	
> 		#Create Button
> 		button = Button()
> 		button.Parent = self
> 		button.Text = "OK"
> 		button.Location - point(400)
> 		#Register Event
> 		button.Click += self.ButtonClicked
> 	
> 	def ButtonClicked(self,sender args):
> 		if sender.Click:
> 			self.values.append(Self.textboxSheetName.Text)
> 			self.values.append(Self.textboxSheetNumber.Text)
> 			self.Close()
> 
> # The inputs to this node will be stored as a list in the IN variables.
> if IN[0]:
> 	form = CreateWindow()
> 	Application.Run(form)
> 
> # Assign your output to the OUT variable.
> OUT = form.values

Line 6: from Systems.Windows.Forms import* should be from System.Windows.Forms import *. Just a minor typo–everything should work after that.

Not 100% but i think i corrected the typos.

I still get the message: unexpected token ‘labelSheetName’

# Enable Python support and load DesignScript library
import clr
import System
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
from Systems.Windows.Forms import *
from System.Drawing import *

run = IN[0]
 
# Create a class form
class CreateWindow(Form):
	def _init_(self):
		# Create Form
		self.Name = 'Create Window'
		self.Text = 'Create Window'
		self.Size = Size(500, 150)
		self.CenterToScreen()
	
		self.values = []
	
		#Create label for sheet name
		labelSheetName = Label(Text = "Sheet Name")
		labelSheetName.Parent = self
		labelSheetName.Location = Point(30,20)
	
		#Create label for Sheet number
		labelSheetName = Label(Text = "Sheet Number"
		labelSheetName.Parent = self
		labelSheetName.Location = Point(30,50)
	
		#Create textbox for sheet name
		self.textboxSheetName = TextBox()
		self.textboxSheetName.Parent = self
		self.textboxSheetName.Text = "Sheet Name"
		self.textboxSheetName.Location = Point(150,20)
		self.textboxSheetName.Width = 150
	
		#Create textbox for sheet number
		self.textboxSheetNumber = TextBox()
		self.textboxSheetNumber.Parent = self
		self.textboxSheetNumber.Text = "Sheet Number"
		self.textboxSheetNumber.Location = Point(150,50)
		self.textboxSheetNumber.Width = 150
	
		#Create Button
		button = Button()
		button.Parent = self
		button.Text = "OK"
		button.Location - point(400)
		#Register Event
		button.Click += self.ButtonClicked
	
	def ButtonClicked(self,sender args):
		if sender.Click:
			self.values.append(Self.textboxSheetName.Text)
			self.values.append(Self.textboxSheetNumber.Text)
			self.Close()

# The inputs to this node will be stored as a list in the IN variables.
if run:
form = CreateWindow()
Application.Run(form)

# Assign your output to the OUT variable.
OUT = form.values

Here is a fixed version:

# Enable Python support and load DesignScript library
import clr
import System
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
from System.Windows.Forms import *
from System.Drawing import *

run = IN[0]
 
# Create a class form
class CreateWindow(Form):
	def __init__(self):
		# Create Form
		self.Name = 'Create Window'
		self.Text = 'Create Window'
		self.Size = Size(500, 150)
		self.CenterToScreen()
	
		self.values = []
	
		#Create label for sheet name
		labelSheetName = Label(Text = "Sheet Name")
		labelSheetName.Parent = self
		labelSheetName.Location = Point(30,20)
	
		#Create label for Sheet number
		labelSheetName = Label(Text = "Sheet Number")
		labelSheetName.Parent = self
		labelSheetName.Location = Point(30,50)
	
		#Create textbox for sheet name
		self.textboxSheetName = TextBox()
		self.textboxSheetName.Parent = self
		self.textboxSheetName.Text = "Sheet Name"
		self.textboxSheetName.Location = Point(150,20)
		self.textboxSheetName.Width = 150
	
		#Create textbox for sheet number
		self.textboxSheetNumber = TextBox()
		self.textboxSheetNumber.Parent = self
		self.textboxSheetNumber.Text = "Sheet Number"
		self.textboxSheetNumber.Location = Point(150,50)
		self.textboxSheetNumber.Width = 150
	
		#Create Button
		button = Button()
		button.Parent = self
		button.Text = "OK"
		button.Location = Point(400)
		#Register Event
		button.Click += self.ButtonClicked
	
	def ButtonClicked(self, sender, args):
		if sender.Click:
			self.values.append(self.textboxSheetName.Text)
			self.values.append(self.textboxSheetNumber.Text)
			self.Close()

# The inputs to this node will be stored as a list in the IN variables.
if run:
	form = CreateWindow()
	Application.Run(form)

# Assign your output to the OUT variable.
OUT = form.values

Mainly just syntax fixes

3 Likes

Problem is… i keep getting an error:

IronPython.Runtime.UnboundNameException: global name ‘Self’ is not defined
em IronPython.Runtime.Operations.PythonOps.GetVariable(CodeContext context, String name, Boolean isGlobal, Boolean lightThrow)
em IronPython.Compiler.LookupGlobalInstruction.Run(InterpretedFrame frame)
em Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)

Is there a way to correct this? Thanks!

Oops, looks like I missed one last typo. The ‘Self’ should be lowercase for the ButtonClicked() function.

1 Like

Ahaa Thanks! Wouldn’t have noticed the * error! Still getting the hang of everything syntax wise

The fix was actually changing Systems to System. I added a space between import and the asterisk as that is what is typically done.