Checkbox
Checkboxes are used to give a user a binary choice, such as yes/no or true/false. They consist of a square box that can be checked or unchecked, and an optional label that describes the choice.
Examples
Tell me a secret
In this example, the on_change() event of the checkbox is used to show or hide a secret message when the user checks or unchecks the box.
import gooeypie as gp
def toggle_secret(event): secret_entry.toggle()
app = gp.GooeyPieApp('Secret')app.width = 300
# Create widgetsquestion_lbl = gp.Label("What's your secret?")question_lbl.style.font_size = 16
secret_entry = gp.Secret()secret_chk = gp.Checkbox("Show secret")
# Set event listener for checkbox to show or hide the secretsecret_chk.on_change(toggle_secret)
# Add widgets to windowapp.add(question_lbl, 1, 1)app.add(secret_entry, 1, 2, expand_horizontal=True)app.add(secret_chk, 1, 3)
app.run()Simple quiz
This example shows how to use several checkboxes to create a simple quiz with multiple answer questions.
import gooeypie as gp
def check_answer(event): """Checks the answers and gives feedback in a popup.""" if python_chk.checked and java_chk.checked and not html_chk.checked and not esperanto_chk.checked: app.alert("Correct", "You got it!", "info") else: app.alert("Incorrect", "Not quite - try again!", "error")
app = gp.GooeyPieApp("Quiz time!")
# Create question label and checkboxesquestion_label = gp.Label("Which of the following are programming languages?")python_chk = gp.Checkbox("Python")html_chk = gp.Checkbox("HTML")esperanto_chk = gp.Checkbox("Esperanto")java_chk = gp.Checkbox("Java")
# Create question frame and add question and checkboxesquestion_frame = gp.Frame()question_frame.add(question_label, 1, 1, align_horizontal="left")question_frame.add(python_chk, 1, 2, align_horizontal="left")question_frame.add(html_chk, 1, 3, align_horizontal="left")question_frame.add(esperanto_chk, 1, 4, align_horizontal="left")question_frame.add(java_chk, 1, 5, align_horizontal="left")
# Answer buttoncheck_answer_btn = gp.Button("Check Answer", check_answer)
# Add question frame and button to appapp.add(question_frame, 1, 1)app.add(check_answer_btn, 1, 2)
app.run()Creating a Checkbox widget
my_check = gp.Checkbox(label_text, checked)Parameters
-
label_text(string): Optional. The text of the checkbox label. -
checked(boolean): Optional. Whether the checkbox is initially checked. Default isFalse.
Properties
| Name | Type | Description |
|---|---|---|
.checked | boolean | Gets or sets whether the checkbox is checked. |
.text | string | Gets or sets the text of the checkbox label. |
.width | integer | Gets or sets the width of the checkbox in pixels. This is the total width of the checkbox, including the label. |
.height | integer | Gets or sets the height of the checkbox in pixels. This is the total height of the checkbox, including the label. |
.size | integer | Gets or sets the size of the checkbox in pixels. This is the width and height of the square box, not including the label. |
.disabled | boolean | Gets or sets whether the checkbox is disabled. When disabled is set to True, the checkbox does not respond to click events. |
Methods
| Name | Returns | Description |
|---|---|---|
.toggle() | None | Toggles the checked state of the checkbox. |
Events
This widget supports the following events as well as the standard events listed below.
| Name | Description |
|---|---|
.on_change(event_function) | The function to call when the checked state of the checkbox changes. This event is triggered whenever the user clicks on the checkbox or when the checked state is changed programmatically. |
Standard events
.on_click.on_double_click.on_right_click.on_middle_click.on_mouse_down.on_mouse_up.on_mouse_enter.on_mouse_leave
Styles
How to specify colors
- Colors can be specified as either a hex code (e.g.
"#FF0000"), a CSS color name, or"transparent". - When specifying a color for a style, you can provide either a single color or two colors separated by a comma. If you provide two colors, the first will be used for light mode and the second for dark mode. For example,
my_widget.text_color = "steelblue", "skyblue"would specify steelblue for light mode and sky blue for dark mode.
| Name | Description | Default |
|---|---|---|
.style.border_color | The color of the border of the checkbox. | |
.style.border_width | The width of the border of the checkbox. | |
.style.checkbox_color | The color of the square box of the checkbox. | |
.style.checkbox_hover_color | The color of the square box of the checkbox when it is hovered over. | |
.style.checkbox_disabled_color | The color of the square box of the checkbox when it is disabled. | |
.style.corner_radius | The radius of the corners of the square box of the checkbox. | 6 |
.style.text_color | The color of the text of the checkbox label. | |
.style.text_disabled_color | The color of the text of the checkbox label when it is disabled. | |
.style.font_name | The font of the text of the checkbox label. Either an installed font or a generic font name: "serif", "sans-serif", "monospace" or "system" | "system" |
.style.font_size | The size of the text of the checkbox label. | 12 |
.style.font_weight | The weight of the text of the checkbox label. Either "normal" or "bold". | "normal" |
.style.font_style | The style of the text of the checkbox label. Either "normal" or "italic". | "normal" |