- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to detect when an OptionMenu or Checkbutton changes in Tkinter?
Let us suppose that in a particular application, we have some fixed set of options or choices for the user in a drop-down list. The Options or Choices can be created using the OptionMenu Widget Constructor.
OptionMenu(window, variable, choice1, choice2, choice3……)
Once the option is created, it can be detected by a click event which generally prints whether a particular option is selected or not. For this example, we will simply create an application where a check button will be present with some choices from the range (1 to 9). By default, the button is set to “1” using the set method. Selecting other options will print the button on the screen.
#Import the tkinter library from tkinter import * #Create an instance of tkinter frame tk = Tk() tk.geometry("700x300") #Create the option and Check Button Event def OptionMenu_CheckButton(event): print(var.get()) pass #Create the variables var = StringVar();var.set("1") options = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] OptionMenu(tk, var, *(options), command = OptionMenu_CheckButton).pack() tk.mainloop()
Running the above code will trace the options selected by the user and print it on the screen.
- How to disable checkbutton Tkinter (grey out)?
- How to make an OptionMenu maintain the same width using tkinter?
- What is the jQuery event to detect when element position changes?
- How to disable (grey out) a checkbutton in Tkinter?
- Making Menu options with Checkbutton in Tkinter?
- How to call a function using the OptionMenu widget in Tkinter?
- When and how to use pack or grid layouts in tkinter?
- How to change the menu background color of Tkinter's OptionMenu widget?
- How to detect if an iOS application is in background or foreground?
- How do I get the 'state' of a Tkinter Checkbutton?
- How to use Swift to detect when AVPlayer video ends playing?
- Detect when an Element Gets Fixed in CSS position:sticky using Intersection Observer
- How to find the number of changes when tossing a coin in R?
- How to detect calls in an android device?