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 |
---|---|
|
Sets the starting color to display in the color picker. |
|
Specifies the parent of the dialog. The dialog is displayed on top of the parent. |
|
Sets the text to be displayed in the dialog title bar. |
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):
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.
3.141.28.107