Creating a button widget

Through the button the user is provided with a means to interact with the GUI and our program. This is accomplished through the button's ability to manually execute commands in your application. These actions may be default, as in the exit command, or custom procedures that we have written.

The keywords are described in the Tk main pages as follows:

Standard Keywords

Interpretation

-activebackground

Specifies the background color to be used when drawing the element. The active background is the color used when the mouse is over the element and when pressing the mouse button will initiate an action.

-activeforeground

Specifies the foreground color to be used when drawing the element. The active foreground is the color used when the mouse is over the element and when pressing the mouse button will initiate an action.

anchor

Specifies how the information (text, bitmap, and so on) is displayed within the widget. Acceptable vales are:

n north, top

ne northeast, top right

e east, right-hand side

se southeast, bottom right

s south, bottom

sw southwest, bottom left

w west, left-hand side

nw northwest, top right

center - center

-background or bg

Specifies the background color to be used when drawing the element.

-bitmap

Specifies a bitmap to display within the element.

-borderwidth or -bd

Specifies a non-negative value indicating the width of the 3D border to draw around the outside of the window.

-compound

Specifies if the widget should display both text and bitmaps/images simultaneously and the placement of where to display the image in relation to the text. Acceptable values are:

none, bottom, top, left, right, or center (default).

-cursor

Specifies the mouse cursor to be used for the window.

-disabledforeground

Specifies the color to use when displaying a disabled element.

-font

Specifies the font to use when drawing the element.

-foreground or fg

Specifies the normal foreground color to be used when drawing the element.

-highlightbackground

Specifies the color to display in the traversal highlight region when the window does not have the input focus.

-highlightcolor

Specifies the color to use for the traversal highlight rectangle that is drawn around the window when it has the input focus.

-highlightthickness

Specifies a non-negative value indicating the width of the highlight rectangle to draw around the outside of the window.

-image

Specifies the image to display within an element. The image must first have been created using the image create command.

-justify

When multiple lines of text exist, this keywords specifies the justification to apply within the element. Acceptable values are: left, center, or right.

-padx

Specifies a non-negative value indicating how much extra space to request for the window in the X-direction.

-pady

Specifies a non-negative value indicating how much extra space to request for the window in the Y-direction.

-relief

Specifies the 3D effect desired for the window. Acceptable values are raised, sunken, flat, ridge, solid, and groove.

-repeatdelay

Specifies the number of milliseconds a key or element must be held down before it will auto-repeat.

-repeatinterval

Used in conjunction with repeatdelay, this keyword specifies the interval between auto-repeats in milliseconds.

-takefocus

Determines whether or not the window accepts the focus during keyboard traversal.

-text

Specifies a string to be displayed within the element.

-textvariable

Specifies the name of a text variable that contains text to be displayed within an element.

-underline

Specifies the integer index of a character to be underlined, zero-based.

-wraplength

Specifies the maximum line length at which point the text will be wrapped for those elements that support word wrap.

-command

Specifies a Tcl command to be activated by a button.

-default

Specifies the default state of the element (See state).

-height

Specifies the desired height for the window.

-overrelief

Specifies an alternate relief for a button to display during mouse-over.

-state

Specifies the state of the widget. Acceptable values are normal, active, and disabled.

-width

Specifies the desired width for the window.

How to do it…

In the following example, we will create a button with specific keywords designed to update the text in a label widget. Enter the following commands:


1 % proc updater { } {
	.l configure -text "Updated"
}
2 % label .l -width 70 -borderwidth 3 -text Original
.l

3 % button .b -text Update -command updater
.b

4 % pack .l -side top
5 % pack .b -side bottom

At this point, your window should look like the following:

How to do it…

How it works…

Based on the keywords provided, we have created a button widget named .b with the text set to Update tied to our procedure named updater. By clicking the button, we configure the text displayed in the label named .l to the string Updated.

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

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