1

I have a menu:

class NodePopup(tk.Menu):
    def __init__(self, master):
        super().__init__(master, tearoff=0)
        self.send_disabled = tk.BooleanVar()
        self.add_checkbutton(label="Disable sending", 
            command=self.toggle_send, variable=self.send_disabled)

    def popup(self, node, event):
        self.send_disabled.set(node.some_flag)
        print('send_disabled:', self.send_disabled.get())
        self.post(event.x_root, event.y_root)


class View:
    def __init__(self, master)
        self.master = master
        self.canvas = #...
        # ...
        self.node_popup = NodePopup(self.master)
        self.canvas.bind("<Button-2>", self.popup)

    def popup(self, event):
        node_oval, node = self.find_node(event.x, event.y)
        self.node_popup.popup(node, event)

When calling my_menu.popup(e), it always pops up without a checkmark. print() prints the right values though.

OS X 10.12, python 3.6.0b4.

What is the problem?

4
  • you run self.node_popup.popup(event, node) with two arguments but def popup(self, event): expects only one. And what do you do in self.toggle_send ? Commented Dec 14, 2016 at 0:10
  • The code works for me when I fix all the mistakes, add the missing code, and set self.send_disabled to True or False. Commented Dec 14, 2016 at 0:14
  • I test on this example and it works for me (Linux Mint). Maybe it problem with OSX. Commented Dec 14, 2016 at 0:15
  • Thanks @furas! That is one last step I should have make (if I had Linux at hand). I ran the example, and there's no tick on the menu either. Clearly, the answer is that it's a OS X port bug. I'll go see where to report it. Commented Dec 14, 2016 at 1:37

2 Answers 2

2

As this code is reported to work on Linux, I reported a bug: http://bugs.python.org/issue28966, which in turn was closed.

Due to a known problem, Python for OS X comes without a built-in Tk, and uses outdated Tk distribution from Apple. One needs to install an ActiveTcl.

As a workaround, I changed the text the way it's clear that the meaning of menu item changed:

self.entryconfig(
    0, label='Enable sending' if node.m_bSendOff else 'Disable sending')
Sign up to request clarification or add additional context in comments.

1 Comment

you can remove def change() in reported code - it was used only by Button() to change checkbutton.
1

If you look at any working example of Menu.add_checkbutton, you'll see that it is using an IntVar, not a BooleanVar. The state of a Tk checkbutton is, in fact, not a boolean: it takes on the values specified by the offvalue and onvalueoptions, which are 0 and 1 by default but could be set to any value.

2 Comments

offvalue/onvalue can be True/False or any strings too. Try offvalue="Hello", onvalue="World" and StringVar().
Tried it and it didn't work either. Maybe it's a bug in particular tkinter version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.