Displaying the color picker

The tk_chooseColor command displays a dialog window that allows the user to select a color. The actual appearance of the color picker will vary, based on the display manager in use. The return hexadecimal value of the color selected is returned. If the user clicks on the Cancel button, an empty string is returned.

The syntax is as follows:

	tk_chooseColor option value …

The tk_chooseColor command accepts one or more option value pairs as detailed in the following table. If no option value pairs are provided, the command will display a dialog with the default values.

Option

Interpretation

-initialcolor color

Sets the starting color to display in the color picker.

-parent window

Specifies the parent of the dialog. The dialog is displayed on top of the parent.

-title string

Sets the text to be displayed in the dialog title bar.

How to do it…

In the following example, we will create a frame and then display a color picker dialog and assign the return value to our frame as the background color. Enter the following commands:


1 % frame .f -width 250 -height 250 -borderwidth 3 -background white
.f

2 % pack .f
3 % .f configure -background [tk_chooseColor -initialcolor gray -title 
"Pick A Color"]

You should see the Tk window with the frame .f displayed following the second command line entry. After you enter the third command line, you will see the color picker dialog (refer to the next screenshot):

How to do it…

Note that in your shell window, the input line is no longer active. This is due to the fact that the application (in this case, the wish shell) is awaiting a response. Select a color and click on the OK button and you will see that the background color of your frame has been changed to the color selected.

How it works…

Based on the option value pairs provided, Tk has displayed a tk_chooseColor dialog. When a color has been selected, the value is returned; in this case, to the configure command for the frame widget.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.28.107