- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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 make an OptionMenu maintain the same width using tkinter?
- How to disable checkbutton Tkinter (grey out)?
- How to disable (grey out) a checkbutton in Tkinter?
- How to call a function using the OptionMenu widget in Tkinter?
- Making Menu options with Checkbutton in Tkinter?
- How to change the menu background color of Tkinter's OptionMenu widget?
- When and how to use pack or grid layouts in tkinter?
- How do I get the 'state' of a Tkinter Checkbutton?
- How to detect if an iOS application is in background or foreground?
- How to add border line below when value changes in an Excel column?
- How to align checkbutton in ttk to the left side?
- How to detect calls in an android device?
- How to accept or reject all changes in Excel?
- How to detect when an Android app goes to the background and come back to the foreground?