Translating Windows Form box from VB.Net to Python

Hi, im pretty new to this forum, Im been trying to translate a VB script of a button, to python, but i haven’t succeed, i was wondering if someone knows a good source for an IronPython introduction, my intuition tells me that the problem lays in the way im assigning the classes(BCL).It seems to me that some classes have multiple ways in which they can be assign. This is the kind error that Python Editor shows :

Runtime error (MissingMemberException): static property ‘Location’ of ‘Form’ can only be assigned to through a type, not an instance
Traceback:
line 9, in scriptButton.gh (11.3 KB)

Hi @Antonio_Cersosimo

there are two ways in GhPython for Rhino 5 to store local state (in your case, the Form object):

I used the second method to fix your code:

import System
import System.Windows.Forms as wf

if "frm" not in globals() or frm.IsDisposed:
    frm = System.Windows.Forms.Form()
    lbl = System.Windows.Forms.Label()
    lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    lbl.AutoSize = True
    frm.Controls.Add(lbl)
    frm.AutoSize = True
    frm.AutoSizeMode = wf.AutoSizeMode.GrowOnly
    frm.ShowInTaskbar = False
    frm.BackColor = System.Drawing.Color.Gray
    frm.TransparencyKey = System.Drawing.Color.Gray
    frm.FormBorderStyle = wf.FormBorderStyle.None
    frm.TopMost = True
    frm.SetDesktopLocation(textposition.X, textposition.Y)
    counter = 0

if run:
    if not frm.Visible: frm.Show()
    #frm.Refresh()
    frm.Location = System.Drawing.Point(textposition.X,textposition.Y)
    frm.Font = System.Drawing.Font(str(fonttype), float(fontsize))
    lbl.ForeColor = fontColor
    lbl.Text = str(textToDisp)
    counter += 1
else:
    frm.Hide()
    counter = 0

button-ok.gh (8.7 KB)

I hope this helps,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi, thanks for the fast reply, I seem to understand pretty much everything, except from where globals() is call, and why the VB Script make use of the counter in the “If statement”, apparently is not necesary.

Python in Grasshopper is interpreted and procedural and you are running under one single scope. Basically, every time GhPython needs to run your code, it will execute all operations, one by one. globals() is the dictionary with all variables defined in the global scope of the module you are in.

The part of the If statement had a bug, it would redefine parts. If you close the window using Alt+Tab, the script fails to work. The new version keeps on working.

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

In other words, globals() is a built-in function.

Well, I been trying to developed a Custom Windows Forms Control, but I’m stuck in defining the method OnPaint(System.Windows.Forms.PaintEventArgs), i don’t know how it can be translate to IronPython and haven’t found a good examples for creating custom controls in IronPython.
Can someone give an example of how the System.Drawing.Graphics Classes are use?. and how a custom control should be define?

I have attach the original Vb.net Script and some fail scripts for translating the myToggle() Class.ThanksButtonToggle.gh (10.0 KB)

I really don’t think this is something that should be done directly in the component. If you want to do this, I would suggest importing a .py file from the same folder where the GH definition is saved. There’s a Windows Forms designer in SharpDevelop that writes Windows Forms controls for you.

Alternatively, consider using Eto to create forms that work both on Windows and on the Mac.

Hi Giulio, yes,what I’m really trying to do is some components for UI design, so at some point is better to separate it into other modules, thanks for SharpDevelop, I was searching for something similar, but I could only find IronPythonStudio, it works with Visual Studio 2008, so it’s a pain.

So i was able to find a good set of tutorials on the OnPaint() Method for Python in here. (http://zetcode.com/tutorials/ironpythontutorial/painting/). I was able to create basic graphics for a control, but i could only change the properties outside the OnPaint() Method.

I was wondering if you know a way to change those properties inside OnPaint(), to use a Color Swatch to change the FillRectangle(Brushes.Red, 20, 20, 120, 80) in the example attach. ThanksRectangle.gh (7.1 KB)

Hi @Antonio_Cersosimo

This is the second example posted on the website you linked, working in GhPython.

on-paint.gh (3.4 KB)

Here attached is all the examples working in GHPython(of the linked website). I was able to change the color in the OnPaint(), I was missing the refresh() method in the If Statement.OnPaint_2.gh (12.1 KB)

2 Likes