Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 9 - GUI Programming using Tkinter - Section 9.4 - The Widget Classes - Check Point - MyProgrammingLab - Page 280: 9.8

Answer

To create a check button in Tkinter with the text "Apple", a white foreground, a red background, associated with a variable `v1`, and with a callback function `processApple`, you can use the following code: import tkinter as tk def processApple(): $\hspace{4mm}$print("Apple check button is", v1.get()) root = tk.Tk() v1 = tk.IntVar() apple_checkbutton = tk.Checkbutton(root, text="Apple", bg="red", fg="white", variable=v1, command=processApple) apple_checkbutton.pack() root.mainloop()

Work Step by Step

In this code, `root` is the main window of your Tkinter application, which is created using the `Tk()` method. Then, we define the callback function `processApple` that will be executed when the check button's state changes. Next, we create a variable `v1` of type `IntVar` to associate with the `check button`, so that its state can be accessed. Then, we create a check button widget using the `Checkbutton` constructor and specify the parent window as `root`, the text as "Apple", the background color as "red" using the `bg` option, the foreground color as "white" using the `fg` option, the variable to be associated as `v1` using the `variable` option, and the command to be executed as `processApple` using the `command` option. Finally, we use the `pack()` method to display the check button on the screen.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.