From The Battle for Wesnoth Wiki
< LuaAPI‎ | types

The widget userdata offers access to a widget of a GUI2 dialog. While there is only one type of widget userdata that covers all widgets including the window itself, the properties of a widget userdata are different for each type of widget. Indexing a widget's userdata can either be used to access a child widget or to set or get a property of a widget. Some properties are read-only; the properties depend on the type of the widget.

An example of accessing a child widget:

function preshow(dialog)
  local okay_button = dialog.okay_button
  -- okay_button is now a handle to the the widgets child with the id 'okay_button' 

Widget Attributes


  • widget.selectedboolean
  • Available on: [toggle_button], [toggle_panel]

Whether the item is selected or not. Note that this should only be used for widgets that have only 2 states. In particular, there exist 3-State toggle_buttons (for example in listbox headers). For those, selected_index must be used instead.


  • widget.selected_indexindex
  • Available on: [listbox], [multi_page], [stacked_widget], [menu_button], [toggle_button], [toggle_panel]

The selected index of the item. For [toggle_button] and [toggle_panel], this is the same as selected only encoded as a number (1 for false or 2 for true) instead of a boolean.


  • widget.texttext
  • Available on: [text_box]

The text of the textbox.


  • widget.valueposition
  • Available on: [slider]

The current position of the slider.


  • widget.percentageposition
  • Available on: [progress_bar]

The current position of the progress bar, between 0 and 100.


  • widget.selected_item_patharray of indices
  • Available on: [treeview]

A table describing the currently selected node. If for example, in the following treeview, Item 9 is selected, the result will be {2,1,3}.



  • widget.patharray of indices
  • Available on: [tree_view_node]

A table describing this node in the overall treeview. See selected_item_path for the meaning of the table..


  • widget.unfoldedboolean
  • Available on: [tree_view_node]

Control whether a tree node is currently expanded or not.


  • widget.unitunit or unit type
  • Available on: [unit_preview_pane]

Change the displayed unit or unit type in the preview pane.


  • widget.item_countnumber of items
  • Available on: [multi_page], [listbox]

The number of items in the container widget.


  • widget.use_markupboolean
  • Available on: Most widgets, in particular [label], [button]

Sets whether the widget's label will parse Pango formatting.


  • widget.labeltext
  • Available on: Most widgets, in particular [label], [button], [image]

The widget's label. Technically this is a special string used in the widget's wml definition. It usually does what one would expect, but also sets the image for image widgets.


  • widget.marked_up_texttext
  • Available on: Most widgets, in particular [label], [button]

Shortcut for setting label and use_markup=yes.


  • widget.enabledboolean
  • Available on: All widgets


  • widget.tooltiptext
  • Available on: All widgets


  • widget.visiblevisibility string
  • Available on: All widgets

Determines whether the widget is visible onscreen. The following visibility statuses are recognized:

String value Boolean shorthand Meaning
visible true The widget is visible and handles events.
hidden The widget is not visible, doesn't handle events, but still takes up space on the dialog grid.
invisible false The widget is not visible, doesn't handle events, and does not take up space on the dialog grid.


  • widget.typestring
  • Available on: All widgets

Returns a string specifying the type of the widget.

Widget callbacks


  • widget.on_modifiedfunction()
  • Available on: Most widgets, in particular [slider], [toggle_button], [listbox], [menu_button], [text_box]

Triggers when the user changes the value of the widget.


  • widget.on_left_clickfunction()
  • Available on: All widgets

Triggers when the user clicks on the widget.


  • widget.on_button_clickfunction()
  • Available on: [button], [repeating_button]

Triggers when the user clicks on the button. This can differ from on_left_click, depending on the type of widget. For example, on a [repeating_button] it will fire multiple times if the user holds the mouse button down.

Widget methods

Any function defined in the gui.widget module and taking a widget as its first parameter can be called as a method of a widget. This includes any functions that are added to the module by user code. Note that these methods are available even if the widget itself doesn't support that function, so in some cases it may be necessary to check widget.type befor calling the method.

This page was last edited on 23 July 2021, at 05:08.