Home > Software engineering >  How do I select checkmark based on highlighted text in tkinter?
How do I select checkmark based on highlighted text in tkinter?

Time:02-20

I have a checkmark and a button, where I by default am able to highlight the text of the checkmark using TAB. I am trying to bind enter to the button, with the exception that the checkmark is highlighted. Is there a method to retrieve this information? Here, is a minimal working example, where I have made up the method "ishighlighted" for the sake of argument:

import tkinter as tk
def something_else()
    if checkmark.ishighlighted():
        checkmark.select()
    else:
        print('Hello World!')
root = tk.Tk()

checkmark = tk.Checkbutton(text = 'Press enter')

checkmark.pack()
do_something_else = tk.Button(text = 'Do something else', command = something_else)
do_something_else.pack()
root.bind('<Return>', lambda event: something_else())
root.mainloop()

CodePudding user response:

You are throwing away the event parameter which can tell you which widget received the event.

def something_else(event):
    if event.widget == checkmark:
        checkmark.select()
    else:
        print('Hello World!')
...
root.bind('<Return>', something_else)
  • Related