qpushbutton - conventional button

Name

qpushbutton — conventional button

Syntax

qpushbutton text [ cmds ...]

Description

The qpushbutton command creates a QPushButton object. This is the conventional button found in graphical user interfaces. The button typically has a text and an associated action that is activated when pushed. The button text may also be set using the qsettext command.

Arguments

text

The button text.

cmds

One or more commands to be performed when the button is pushed. The commands must be a string or a list, adhering to Tcl's syntax. A string may be either a single word or several words grouped as one string using quotes; a list is delimited by the '{' and '}' characters. See examples below.

An action assigned for later execution is called a callback function. Since Tcl uses strings and lists to store commands, we will often use the term 'callback string' or 'callback list'.

Examples

Ex.1: QPushButton

proc button_callback {} {
	qinformation "Button callback" Hello
}

# Create toplevel window
set w [qwidget ]

# Delete window from memory when closing
qsetattribute deleteonclose

qsetwindowtitle "QPushButton"

# Arrange contents vertically
qvboxlayout {
	# Create a button that show an information dialog when pushed
	qpushbutton "Hello world!" button_callback
}

# Make window visible
qshow $w

Example QPushButton shows a QPushButton that will call the user-defined procedure 'ok' when pushed.

Ex.2: Argument button

proc button_callback { infotext } {
	qinformation "Button callback" $infotext
}

# Create toplevel window
set w [qwidget ]

# Delete window from memory when closing
qsetattribute deleteonclose

qsetwindowtitle "QPushButton"

# Arrange contents vertically
qvboxlayout {
	# Create a button that show an information dialog when pushed
	qpushbutton "Hello world!"  { button_callback Hello }
}

# Make window visible
qshow $w

Example Argument button shows how to supply user arguments to the button callback. The text to be displayed in the information dialog is provided as an argument to the button_callback procedure.

Ex.3: Resolved argument button

proc button_callback { infotext } {
	qinformation "Button callback" $infotext
}

# Create toplevel window
set w [qwidget ]

# Delete window from memory when closing
qsetattribute deleteonclose

qsetwindowtitle "QPushButton"

# Arrange contents vertically
qvboxlayout {
	# Create a button that show an information dialog when pushed
	set infotext Hello
	qpushbutton "Hello world!"  "button_callback $infotext"
	set infotext Goodbye ; # No effect
}

# Make window visible
qshow $w

Example Resolved argument button shows how to assign a callback string where the variables contained within are resolved immediately. When qpushbutton is called Tcl will resolve infotext to its value "Hello" before assigning the resulting string as a callback string to the button. Subsequently, changing the value of x has no effect. This behavior results from the use of quote operators to delimit the string. If instead a '{...}' delimited command list was assigned to the button, the variable infotext would not be resolved, as shown in example Delayed argument button. Here infotext is resolved in button_callback at whatever time the user presses the button.

Ex.4: Delayed argument button

proc button_callback { infotext } {
	qinformation "Button callback" $infotext
}

# Create toplevel window
set w [qwidget ]

# Delete window from memory when closing
qsetattribute deleteonclose

qsetwindowtitle "QPushButton"

# Arrange contents vertically
qvboxlayout {
	# Create a button that show an information dialog when pushed
	set infotext Hello
	qpushbutton "Hello world!"  { button_callback $infotext }
	set infotext Goodbye
}

# Make window visible
qshow $w

Ex.5: Callback block

# Create toplevel window
set w [qwidget ]

# Delete window from memory when closing
qsetattribute deleteonclose

qsetwindowtitle "QPushButton"

# Arrange contents vertically
qvboxlayout {
	# Create a button that show an information dialog when pushed
	
	qpushbutton "Hello world!" {
		qinformation First Hello
		qinformation Second Goodbye
		
	}
}

# Make window visible
qshow $w

A complicated callback function may be provided as an inline argument to the qpushbutton without resorting to an external procedure. This is shown in example Callback block.