Hi All,
I was trying to Add image to a windows form as shown below
# Add Image
self.PictureBox1 = PictureBox()
self.PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
self.PictureBox1.Image = "Libraries\Pictures\\sample.png"
self.PictureBox1.BorderStyle = BorderStyle.Fixed3D
self.PictureBox1.Size = Size(40,40)
self.PictureBox1.Location = Point(200,300)
below is an example and original code from Danny Bentley
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
import System
from System.Windows.Forms import *
from System.Drawing import *
# Create a class form
class CreateWindow(Form):
def __init__(self):
#Create the 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
labelSheetNumber = Label(Text = "Sheet Number")
labelSheetNumber.Parent = self
labelSheetNumber.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,60)
#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()
if IN[0]:
form = CreateWindow()
Application.Run(form)
OUT = form.values
Thanks.
GregX
September 19, 2018, 3:26pm
2
Try looking at the Data-Shapes package, it has a parametric UI series of nodes that you can use or inspect to create your own code(there is also a node to add an image so you can try and copy some of that code)
The code in the “UI.MultipleInputForm ++” is quite long but you can copy and paste it in another text editor to search for terms like “image”.
1 Like
thanks yeah, im trying but cant get it to work
# Add Image
self._pictureBox1 = PictureBox()
self._pictureBox1.ImageLocation = "C:\\Users\\username\\AppData\\Roaming\\Dynamo\\Dynamo Revit\\1.3\\packages\\Data\\extra\\a.png"
self._pictureBox1.Location = _Point(163, 199)
self._pictureBox1.Name = "pictureBox1"
self._pictureBox1.Size = _Size(100, 50)
self._pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
self.Controls.Add(_pictureBox1)
GregX
September 19, 2018, 5:08pm
4
I tried adding this line of code:
self.Controls.Add(self.PictureBox1)
Looking at the datashapes file I found the controls.add method
Thanks!but i got an unexpected indent
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
import System
from System.Windows.Forms import *
from System.Drawing import *
# Create a class form
class CreateWindow(Form):
def __init__(self):
#Create the 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
labelSheetNumber = Label(Text = "Sheet Number")
labelSheetNumber.Parent = self
labelSheetNumber.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
self.PictureBox = PictureBox()
self.PictureBox.SizeMode = PictureBoxSizeMode.CenterImage
self.PictureBox.Image = Image.FromFile("C:\\Users\\user\\AppData\\Roaming\\Dynamo\\Dynamo Revit\\1.3\\packages\\Data\\extra\\a.png")
self.PictureBox.Location = Point(320, 20)
self.PictureBox.Size = Size(50, 50)
self.Controls.Add(self.PictureBox)
#Create button
button = Button()
button.Parent = self
button.Text = "Ok"
button.Location = Point(400,60)
#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()
if IN[0]:
form = CreateWindow()
Application.Run(form)
OUT = form.values
GregX
September 19, 2018, 5:38pm
6
I too was getting that error from your code, the indents are probably a little messed up:
opened in word
Try selecting the space between each pair of lines regarding the PictureBox, delete the space then press enter
hi can you drop your code? im still getting unexpected indents even if i retype it…when i remove the adding picturebox the code is working,…Many Thanks.
GregX
September 19, 2018, 6:55pm
8
lol here you go
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
import System
from System.Windows.Forms import *
from System.Drawing import *
# Create a class form
class CreateWindow(Form):
def __init__(self):
#Create the 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
labelSheetNumber = Label(Text = "Sheet Number")
labelSheetNumber.Parent = self
labelSheetNumber.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
self.PictureBox = PictureBox()
self.PictureBox.SizeMode = PictureBoxSizeMode.CenterImage
self.PictureBox.Image = Image.FromFile("C:\\Users\\user\\AppData\\Roaming\\Dynamo\\Dynamo Revit\\1.3\\packages\\Data\\extra\\a.png")
self.PictureBox.Location = Point(320, 20)
self.PictureBox.Size = Size(50, 50)
self.Controls.Add(self.PictureBox)
#Create button
button = Button()
button.Parent = self
button.Text = "Ok"
button.Location = Point(400,60)
#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()
if IN[0]:
form = CreateWindow()
Application.Run(form)
OUT = form.values
2 Likes
now it’s working thanks
2 Likes