Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5746

Cannot set background color of ComboBox in Eto Forms

$
0
0

@Terry_Chappell wrote:

The attached, simple script for a form has a ComboBox on the top-right (The list). I want to change its background color. But nothing changes when I add this line to its control definition:

cb.BackgroundColor = Colors.Green

What is the proper way to do this or, despite being listed in the documentation for Eto Forms, does this not work for a ComboBox? Note that changing the background color of other controls on this form does work (see background behind the word: something).

image

ETO Example Revised.py (10.5 KB)

#Purpose: This script will demonstrate how to do different things with an ETO form

import rhinoscriptsyntax as rs
import Rhino
import Rhino.UI
import Eto.Drawing as drawing
#from Eto.Drawing import Colors 
from Eto.Drawing import Bitmap, Size, Font, FontStyle, Color, Colors, FontFamily, ColorHSL
import Eto.Forms as forms
from Eto.Forms import TextAlignment
from System.Collections.Generic import Dictionary
from scriptcontext import sticky

hues = [(233/255.,1,0.01),(233/255.,1,0.05),(233/255.,1,0.08),(233/255.,1,0.13),(233/255.,1,0.18), # 4
(233/255.,1,0.23),(233/255.,1,0.34),(233/255.,1,0.45),(230/255.,1,0.5),(223/255.,1,0.5), # 9
(216/255.,1,0.53), (210/255.,1,0.53),(206/255.,1,0.5),(202/255.,1,0.5),(198/255.,1,0.5), # 14 # Magenta = 213
(194/255.,1,0.5),(190/255.,1,0.5),(186/255.,1,0.5),(182/255.,1,0.5),(178/255.,1,0.5), # 19
(170/255.,1,0.5),(162/255.,1,0.5),(157/255.,1,0.5),(152/255.,1,0.5),(148/255.,1,0.5), # 24 # Blue = 170
(145/255.,1,0.5),(142/255.,1,0.5),(139/255.,1,0.5),(136/255.,1,0.5),(133/255.,1,0.5), #29
(130/255.,1,0.48),(128/255.,1,0.48),(122/255.,1,0.48),(116/255.,1,0.47),(108/255.,1,0.47), #34 # Blue_Green = 128
(100/255.,0.8,0.47),(90/255.,0.6,0.47),(85/255.,0.75,0.47),(68/255.,0.80,0.47),(58/255.,0.9,0.48), #39 # Green = 85
(49/255.,0.9,0.48),(43/255.,1,0.45),(40/255.,1,0.47),(36/255.,1,0.49),(33/255.,1,0.5), # 44 # Yellow = 42
#(30/255.,1,0.5),(27/255.,1,0.5),(24/255.,1,0.5),(21/255.,1,0.5),(18/255.,1,0.5), #49 # Orange = 21
(30/255.,1,0.5),(27/255.,1,0.5),(24/255.,1,0.5),(21/255.,1,0.5),(18/255.,1,0.5), #49 # Orange = 21
(15/255.,1,0.5),(12/255.,1,0.5),(9/255.,1,0.5),(8/255.,0.9,0.4375),(8/255.,0.9,0.375), # 54 # Red = 9
#(15/255.,1,0.5),(12/255.,1,0.45),(9/255.,1,0.4),(8/255.,0.9,0.4),(8/255.,0.9,0.375), # 54 # Red = 9
(8/255.,0.85,0.30),(8/255.,0.85,0.24),(8/255.,0.9,0.19),(8/255.,1,0.14),(8/255.,0.95,0.12), #59
(6/255.,0.8,0.1),(0/255.,0,0.15),(0/255.,0,0.25),(0/255.,0,0.35),(0/255.,0,0.45), #64
(0/255.,0,0.55),(0/255.,0,0.65),(0/255.,0,0.75),(0/255.,0,0.90),(0/255.,0,1.), #69
(0/255.,0,1.)] # Duplicate last color for interpolation of last point.


#================ This section defines sticky variables ========================
class TestDialogArgs():
	# Initializer
	def __init__(self):
		self.ABC = 0
		self.pickABC = 0
		self.expand = False
		self.box_visible = False
		self.box_something = 'Something'

#================ This is the section that defines the form ====================
class TestDialog(forms.Dialog[bool]):
	
	def __init__(self, args):
		if sticky.has_key('test_settings'): self.Args = sticky['test_settings']
		else: self.Args = args
		my_font_size = 10
		# Define the tool tips 
		ToolTips = Dictionary[str,str]()
		ToolTips['A'] = 'Pick one of these letters'
		ToolTips['The list'] = 'This combo box should change based off of the letter'
		ToolTips['OK'] = 'Perform the task'
		ToolTips['Cancel'] = 'Close this task'
		ToolTips['Text'] = 'Type something here'
		ToolTips['Expand'] = 'Expand the form'

		# Build the form 
		self.Title = 'Experiment with ETO'
		self.Padding = drawing.Padding(10)
		self.Resizable = False

		# Create a groupbox for the radio button list
		self.m_groupbox = forms.GroupBox(
			Font = Font('Segoe UI', my_font_size, FontStyle.Bold),
			Text = 'Pick a letter',
			Size = Size(150,90),
			Padding = drawing.Padding(0, 5, 0, 5),
			TextColor = Colors.Brown)
		grouplayout = forms.DynamicLayout()
		grouplayout.Spacing = drawing.Size(5, 5)
		# create a radio button list for the groupbox above
		self.m_radiobuttonlist = forms.RadioButtonList(
			DataStore = ['A', 'B', 'C'],
			ToolTip = ToolTips['A'],
			Orientation = forms.Orientation.Vertical,
			SelectedIndex = self.Args.ABC)
		self.m_radiobuttonlist.SelectedIndexChanged += self.OnRadiobuttonChange  # try to get the combo box to change with the radio buttons
		grouplayout.AddRow(self.m_radiobuttonlist)
		self.m_groupbox.Content = grouplayout
		self.m_radiobuttonlist.Enabled = True

		# create a group for the combo list box
		self.n_groupbox = forms.GroupBox(
			Text = 'The list',
			ToolTip = ToolTips['The list'],
			Padding = drawing.Padding(5, 5, 5, 5),
			Font = Font('Segoe UI', my_font_size, FontStyle.Bold),
			TextColor = Colors.Brown)
		grouplayout = forms.DynamicLayout()
		grouplayout.Spacing = drawing.Size(5, 5)
		# create a combo list box for the group above.
		cb = forms.ComboBox()
		cb.DataStore = ['first pick A', 'pick A', 'third pick A']
		cb.Font = Font('Segoe UI', my_font_size, FontStyle.Bold)
		cb.TextColor = Colors.Blue
		cb.BackgroundColor = Colors.Green
		cb.SelectedIndex = self.Args.pickABC
		cb.Enabled = True
		cb.MouseWheel += self.OnWheel # OK to specify delegate for event here in this format.
		self.n_combobox = cb
		"""
		self.n_combobox = forms.ComboBox(
			DataStore = ['first pick A', 'pick A', 'third pick A'],
			Font = Font('Segoe UI', my_font_size, FontStyle.Bold),
			TextColor = Colors.Blue,
			SelectedIndex = self.Args.pickABC,
			MouseWheel += self.OnMouseWheel) # No legal to include this here.
		"""
		grouplayout.AddRow(self.n_combobox)
		self.n_groupbox.Content = grouplayout

		# Create a label
		cl = forms.Label()
		cl.Text = 'Enter here'
		cl.Font = Font('Segoe UI', 11, FontStyle.Bold)
		cl.TextAlignment = TextAlignment.Left
		cl.TextColor = Colors.Green
		cl.MouseDown += self.OnUp
		self.m_label = cl
		"""
		self.m_label = forms.Label(
			Text = 'Enter here:',
			Font = Font('Segoe UI', 11, FontStyle.Bold),
			TextAlignment = TextAlignment.Left,
			TextColor = Colors.Green)
		"""
		num = 50
		# Create a text box
		self.m_textbox = forms.TextBox(
			Text = self.Args.box_something,
			Font = Font('Segoe UI', 10, FontStyle.Bold),
			TextColor = Colors.Brown,
			BackgroundColor = ColorHSL.ToColor(ColorHSL(hues[num][0],hues[num][1],hues[num][2])),
			ToolTip = ToolTips['Text'])

		# Create a checkbox for expanding the page
		self.n_checkbox = forms.CheckBox(
			Text = 'Expander',
			Checked = self.Args.expand,
			Font = Font('Segoe UI', 12, FontStyle.Bold),
			TextColor = Colors.Red,
			ToolTip = ToolTips['Expand'])
		self.n_checkbox.CheckedChanged += self.OnExpandCheckedChanged
		self.n_checkbox.MouseDoubleClick += self.OnUp
		
		# Create a text box
		self.not_visible = forms.TextBox(
			Visible = self.Args.box_visible,
			Text = 'Was not visible',
			Font = Font('Segoe UI', 10, FontStyle.Bold),
			TextColor = Colors.Brown,
			ToolTip = ToolTips['Text'])

		# main form Okay button
		self.DefaultButton = forms.Button(
			Text = 'OK',
			Font = Font('Segoe UI', 12, FontStyle.Bold),
			TextColor = Colors.Green,
			ToolTip = ToolTips["OK"])
		self.DefaultButton.Click += self.OnOKButtonClick

		# main form Cancel button
		self.AbortButton = forms.Button(
			Text = 'Cancel',
			Font = Font('Segoe UI', 12, FontStyle.Bold),
			TextColor = Colors.Red,
			ToolTip = ToolTips["Cancel"])
		self.AbortButton.Click += self.OnCloseButtonClick

		#layout the form
		layout = forms.DynamicLayout()
		layout.Spacing = drawing.Size(5, 5)
		layout.AddRow(self.m_groupbox, self.n_groupbox)
		layout.AddRow(None)
		layout.AddRow(self.m_label, self.m_textbox)
		layout.AddRow(None)
		layout.AddRow(self.n_checkbox)
		layout.AddRow(None)
		layout.AddRow(self.not_visible)
		layout.AddRow(None)
		layout.AddRow(self.DefaultButton, self.AbortButton)

		self.Content = layout
		
		# These 3 lines set RadioButton font to Bold. Use Default for regular font.
		self.Styles.Add[forms.RadioButton](None, self.setFont)
				
	def setFont(self, radioButton):
		radioButton.Font = drawing.SystemFonts.Bold()

	# this section created functions for the main script
	def GetRadioButton(self):
		return self.m_radiobuttonlist.SelectedIndex
	
	def OnWheel(self, sender, e):
		print e.Delta.Height
		i = 1 if e.Delta.Height > 0 else -1
		next = min(2, max(0, self.n_combobox.SelectedIndex + i))
		print 'Current index = {} next = {}'.format(self.n_combobox.SelectedIndex, next)
		self.n_combobox.SelectedIndex = next

	def OnUp(self, sender, e):
		a = 1
		print a
		
		# this section is for changing the combo list based off of the radio button selection
	def OnRadiobuttonChange(self, sender, e):
		if self.m_radiobuttonlist.SelectedIndex == 0:
			self.n_combobox.DataStore = ['first pick A', 'second pick A', 'third pick A']
			self.n_combobox.SelectedIndex = 0
			print "should be A"
			# does something go here?

		elif self.m_radiobuttonlist.SelectedIndex == 1:
			self.n_combobox.DataStore = ['first pick B', 'second pick B', 'third pick B']
			self.n_combobox.SelectedIndex = 0
			print "should be B"
			
		elif self.m_radiobuttonlist.SelectedIndex == 2:
			self.n_combobox.DataStore = ['first pick C', 'second pick C', 'third pick C']
			self.n_combobox.SelectedIndex = 0
			print "should be C"
	def OnExpandCheckedChanged(self, sender, e):
		if self.n_checkbox.Checked:
			self.not_visible.Visible = True
			self.ClientSize = drawing.Size(self.ClientSize.Width, self.ClientSize.Height + 25)
		else:
			self.not_visible.Visible = False
			self.ClientSize = drawing.Size(self.ClientSize.Width, self.ClientSize.Height - 25)
	
	def GetComboBox(self):
		return self.n_combobox.Text

	def GetText(self):
		return self.m_textbox.Text
		self.Close(False)
		
	def Expander((self, sender, e)):
		self.ClientSize = drawing.Size(200,200)

	def SaveArgs(self):
		# Save latest setting in Args so sticky can recover them at next startup.
		self.Args.ABC = self.m_radiobuttonlist.SelectedIndex
		self.Args.pickABC = self.n_combobox.SelectedIndex
		self.Args.expand = self.n_checkbox.Checked
		self.Args.box_visible = self.not_visible.Visible
		self.Args.box_something = self.m_textbox.Text

	def OnCloseButtonClick(self, sender, e):
		# Save settings.
		self.SaveArgs()
		sticky['test_settings'] = self.Args
		self.Close(True)
		print "This command was cancelled."
		exit()

	def OnOKButtonClick(self, sender, e):
		# Save settings.
		self.SaveArgs()
		sticky['test_settings'] = self.Args
		self.Close(True)

def TestETO():
	#========================= Use the ETO form ===============================
	args = TestDialogArgs()
	dialog = TestDialog(args);
	rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
	letter = dialog.GetRadioButton()
	if letter is None: return
	combo = dialog.GetComboBox()
	if combo is None: return
	test = dialog.GetText()
	if test is None: return

	#============================ test the output ==============================
	print letter
	print combo
	print test

if __name__ == "__main__":
	TestETO()

Regards,
Terry.

Posts: 6

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 5746

Trending Articles