0

I have a valid code

I want to print "Option1" when I clicked to checkbutton1 and print value of related variable, I created raise_menubutton method, how can I identify which variable was called?

import tkinter as tk

root = tk.Tk()
root.geometry("%ix%i" % (50, 50))


def raise_menubutton(var, indx, mode):
    menubutton.menu.post(menubutton.winfo_rootx(), menubutton.winfo_rooty() + menubutton.winfo_height())
    print("QQQ2 ", indx)
    print("QQQ3 ", mode)
    # I want to print "Option1" when I clicked to checkbutton1 and print value of related variable


menubutton = tk.Menubutton(root, text="CheckComboBox", relief="raised")
menubutton.grid()
menubutton.menu = tk.Menu(menubutton, tearoff=0)
menubutton["menu"] = menubutton.menu

Item0 = tk.IntVar()
Item1 = tk.IntVar()
Item2 = tk.IntVar()

# Bind to variables changing
Item0.trace_add("write", raise_menubutton)
Item1.trace_add("write", raise_menubutton)
Item2.trace_add("write", raise_menubutton)

menubutton.menu.add_checkbutton(label="Option0", variable=Item0)
menubutton.menu.add_checkbutton(label="Option1", variable=Item1)
menubutton.menu.add_checkbutton(label="Option2", variable=Item2)

root.mainloop()

When I print var output is "PY_VAR1"

When I print var.get() i hve an error

AttributeError: 'str' object has no attribute 'get'

swor
  • 751
  • 3
  • 8
  • 21
  • 1
    Does https://stackoverflow.com/questions/29690463/what-are-the-arguments-to-tkinter-variable-trace-method-callbacks help? – Karl Knechtel Mar 25 '21 at 20:51
  • Thank you, not at all. How can I print value of related variable? (check button) – swor Mar 25 '21 at 21:05
  • 3
    Is there a reason you're using `trace` instead of the `command` option of the checkbutton? – Bryan Oakley Mar 25 '21 at 21:06
  • 1
    Well, you wanted to know how you can "identify which variable was called", by which you presumably mean, which button was pressed. The linked answer explains that the `var` parameter is set according to the `name` of the button that was pressed (which, among other possibilities, you can set when you create the button). So... – Karl Knechtel Mar 25 '21 at 23:25

0 Answers0