When using tkinter OptionMenu the command input lets you activate a function, but the same function activates even when you click on the already set option. Example: option "A" is set, you click again on option "A" the command activates, however I would like it to happen only if I change the selection from "A" to another option that isn't "A" like "B" or "C" and viceversa.
Here's my code:
import tkinter
from tkinter import *
# Create window------------------------------------------
window = tkinter.Tk()
window.title("Window")
window.geometry("800x500")
#--------------------------------------------------------
# Function-----------------------------------------------
def print_message(*args):
print("the option has been changed")
#--------------------------------------------------------
# Dropdown menu------------------------------------------
# Datatype
option_selected = StringVar()
# Create dropdown menu
option_list = ["A", "B", "C"]
option_selected.set(option_list[0])
option_menu = OptionMenu(window, option_selected, *option_list)
option_menu.pack()
option_selected.trace("w",print_message)
#--------------------------------------------------------
# Maintain window open
window.mainloop()
Basically
OptionMenudoes not support such feature, however you can create a customOptionMenuto provide such feature: