First Python Form - SharpDevelop

Here is the code to my first Python Form using ( SharpDevelop - www.icsharpcode.net/opensource/sd/ )

The code below runs from the EditPythonScript command

How do I get the input text from this Form and place it into a variable - after pressing the Submit button ?

thx Keith

import System.Drawing
import System.Windows.Forms

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

class MainForm(Form):
	def __init__(self):
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._textBox1 = System.Windows.Forms.TextBox()
		self._label1 = System.Windows.Forms.Label()
		self._label2 = System.Windows.Forms.Label()
		self._textBox2 = System.Windows.Forms.TextBox()
		self._button1 = System.Windows.Forms.Button()
		self.SuspendLayout()
		# 
		# label1
		# 
		self._label1.Location = System.Drawing.Point(40, 39)
		self._label1.Name = "label1"
		self._label1.Size = System.Drawing.Size(100, 20)
		self._label1.TabIndex = 4
		self._label1.Text = "label1"
		# 
		# label2
		# 
		self._label2.Location = System.Drawing.Point(40, 88)
		self._label2.Name = "label2"
		self._label2.Size = System.Drawing.Size(100, 20)
		self._label2.TabIndex = 3
		self._label2.Text = "label2"
		# 
		# textBox1
		# 
		self._textBox1.Location = System.Drawing.Point(154, 39)
		self._textBox1.Name = "textBox1"
		self._textBox1.Size = System.Drawing.Size(100, 20)
		self._textBox1.TabIndex = 0
		# 
		# textBox2
		# 
		self._textBox2.Location = System.Drawing.Point(154, 88)
		self._textBox2.Name = "textBox2"
		self._textBox2.Size = System.Drawing.Size(100, 20)
		self._textBox2.TabIndex = 1
		# 
		# button1
		# 
		self._button1.Location = System.Drawing.Point(154, 138)
		self._button1.Name = "button1"
		self._button1.Size = System.Drawing.Size(100, 23)
		self._button1.TabIndex = 2
		self._button1.Text = "Submit"
		self._button1.UseVisualStyleBackColor = True
		self._button1.Click += self.Button1Click
		# 
		# MainForm
		# 
		self.ClientSize = System.Drawing.Size(284, 207)
		self.Controls.Add(self._button1)
		self.Controls.Add(self._label2)
		self.Controls.Add(self._textBox2)
		self.Controls.Add(self._label1)
		self.Controls.Add(self._textBox1)
		self.Name = "MainForm"
		self.Text = "pythontest"
		self.ResumeLayout(False)
		self.PerformLayout()


	def Button1Click(self, sender, e):
		self.Close()

if __name__ == "__main__":
    form = MainForm()
    form.ShowDialog()

Hi Keith,

just some notes to make this work. Currently the form (the submit button) just returns Cancel. To change this you might add this to your button1 section:

# button1
self._button1.DialogResult = System.Windows.Forms.DialogResult.OK

This will make your form return OK only when the submit button is pressed. Together with the dialog result added to your submit button, you can call your form like this and retrieve its textbox values from your form object if the result OK is returned:

if __name__ == "__main__":
    form = MainForm()
    if (form.ShowDialog()) == System.Windows.Forms.DialogResult.OK:
        print(form._textBox1.Text)
        print(form._textBox2.Text)

Btw. some note: the underscore in front indicates usually a class “internal use”, which is now not fully correct anymore :slight_smile:

One handy thing to consider is that when you enter something into your form, the entered content is still “there” even after you have closed and reopened the form. This is true as long as your form object is not disposed using form.Dispose()

btw. you might also define some default text for your textBox… fields using this syntax:

self._textBox1.Text = "rhino"
self._textBox2.Text = "rocks"

c.

1 Like

Thanks for all your help Clement - will this Form also work on a Mac ?

Here is the now-working code as reference:

import System.Drawing
import System.Windows.Forms

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

class MainForm(Form):
	def __init__(self):
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._textBox1 = System.Windows.Forms.TextBox()
		self._label1 = System.Windows.Forms.Label()
		self._label2 = System.Windows.Forms.Label()
		self._textBox2 = System.Windows.Forms.TextBox()
		self._button1 = System.Windows.Forms.Button()
		self.SuspendLayout()
		# 
		# label1
		# 
		self._label1.Location = System.Drawing.Point(40, 39)
		self._label1.Name = "label1"
		self._label1.Size = System.Drawing.Size(100, 20)
		self._label1.TabIndex = 4
		self._label1.Text = "label1"
		# 
		# label2
		# 
		self._label2.Location = System.Drawing.Point(40, 88)
		self._label2.Name = "label2"
		self._label2.Size = System.Drawing.Size(100, 20)
		self._label2.TabIndex = 3
		self._label2.Text = "label2"
		# 
		# textBox1
		# 
		self._textBox1.Location = System.Drawing.Point(154, 39)
		self._textBox1.Name = "textBox1"
		self._textBox1.Size = System.Drawing.Size(100, 20)
		self._textBox1.TabIndex = 0
		# 
		# textBox2
		# 
		self._textBox2.Location = System.Drawing.Point(154, 88)
		self._textBox2.Name = "textBox2"
		self._textBox2.Size = System.Drawing.Size(100, 20)
		self._textBox2.TabIndex = 1
		# 
		# button1
		# 
		self._button1.Location = System.Drawing.Point(154, 138)
		self._button1.Name = "button1"
		self._button1.Size = System.Drawing.Size(100, 23)
		self._button1.TabIndex = 2
		self._button1.Text = "Submit"
		self._button1.UseVisualStyleBackColor = True
		self._button1.Click += self.Button1Click
		self._button1.DialogResult = System.Windows.Forms.DialogResult.OK
		# 
		# MainForm
		# 
		self.ClientSize = System.Drawing.Size(284, 207)
		self.Controls.Add(self._button1)
		self.Controls.Add(self._label2)
		self.Controls.Add(self._textBox2)
		self.Controls.Add(self._label1)
		self.Controls.Add(self._textBox1)
		self.Name = "MainForm"
		self.Text = "pythontest"
		self.ResumeLayout(False)
		self.PerformLayout()

	def save():
		key = self._textBox1.Text() 

	def Button1Click(self, sender, e):
		self.Close()


if __name__ == "__main__":
    form = MainForm()
    if (form.ShowDialog()) == System.Windows.Forms.DialogResult.OK:
        print(form._textBox1.Text)
        print(form._textBox2.Text)

not shure, someone else owning a Mac may have to try out and report here…

c.

I think virtually none of the WindowsForms stuff will work on Mac Rhino. You’ll have to wait until they implement ETO

How does one download and install a copy of SmartDevelop through GitHub? I see the files for SharpDevelop but no executable to launch the software.

Eric

Hi Eric,

Thanks.

Djordje,

Just installed and worked perfectly. Thank you again.

Eric

1 Like

Make sure you download version 4 not 5, as 5 only supports C# not IronPython.

On that subject, @piac how plausible would it be to add ETO support for SharpDevelop gui designer, it’s open-source project?
Currently it can detect WinForms inside any DLL and add it to the toolbox, but it doesn’t detect any forms inside ETO dll.

(feel free to split this into another thread if it requires a dedicated attention. :))

I downloaded version 4. I did read that 5 was for C+ only thank you.

How does one run a sharpdevelop user form from outside of the module. I don’t want to have to paste all that code Into my main module. I’d like to keep it remote and access it from the outside.

The generated code is a class. You can call it from another script, but you do need to add some code in there to call your module.

Thank you Ivelin. So obvious and staring me right in the face. I posted this code into another module and it called the form:

#import the Userform Class
from UserForm_MB2 import GetUFData

form = GetUFData()
if form.ShowDialog() == System.Windows.Forms.DialogResult.Cancel:
print(form._num_Ply)
print(form._num_Length)
print(form._num_Width)
print(form._num_Height)
print(form._num_HingeOff)
print(form._num_ClampOff)
print(form._num_NumTog)
print(form._num_NumHinges)
print(form._cmb_HoleType)
print(form._num_HoleD)