tkinter disable right click on a pop-up menu - python

I have a pop-up menu that comes up when I right click on a widget in tkinter
def popup(event):
self.popup_menu.tk_popup(event,x, event.y, 0)
popup_menu = tk.Menu(widget, tearoff=False)
popup_menu.add_command(label='whatever', command=lambda: print('whatever'))
...
bind('<Button-3>', popup)
It pops up fine, but a command runs even when it is right clicked. I would like to make the command run only when it is left clicked, to prevent weird behavior when releasing the right button.
I've looked through the options here, and there's not too much information elsewhere about pop-up menues. If anyone knows how to disable right-click or a work around pop-up where I can control things like that.
EDIT: my current work around is binding the popup to the release of the right button, so the release can't trigger an event. bind('<ButtonRelease-3>', popup)'

Related

tkinter: Pressing spacebar activates last-used button, even if it's no longer on the screen

I have a button that, when pressed, changes the page by degridding its parent frame and gridding new frames. However, if the user hits space after this button is pressed, it runs the button's command again because the button is still highlighted/selected even though it is no longer visible. This presents some big problems for my code which depends on this button only being pressed once. How can I "clear" the highlighting of a button or at least disable the spacebar from being used to activate buttons?

tkinter button wont release after click

I am new to python development and am currently building a gui using tkinter.
My button works as expected when clicked, opens another window displays face bounding boxes, however when I close the faces window and return to the buttons main window, the button still displays as clicked (pressed/sunken) and I cannot click any other buttons or close the window.
How do I release the button after it is clicked?
def btn1():
os.system("python App.py group.jpg")
button1 = Button(window, text = "Button 1", command=btn1).grid(row=0, column=0)
(SOLUTION)
I discovered that the last line of the App.py script was: cv2.waitKey(0)
The user must hit "Enter" in-order for the commmand/waitKey to terminate.
Closing the window using the 'X' on the window toolbar, does close the window but does not terminate the command and leaves the button in a pressed-state.
I have amended this value to specific time parameter 2500 i.e. 2500 milliseconds so no user involvement is required. Although as stated, simply pressing 'enter/return' terminates the command and releases the button.
To confirm, this works for both os.system and subprocess.
Hope this helps save others sometime, and thanks to everyone for your help.

How to keep an urwid.Edit always in focus?

I have programmed with python+urwid a ircII-like screen, where I have a text flow the entire screen of the terminal, plus an editable text at the bottom as the prompt, to let the user insert commands and press enter.
The main body of the screen is an urwid.SimpleFocusListWalker and for each line of new text (e.g. command response) a new urwid.Text is created.
This code shows how I create the layout.
self._widgetPromptText = urwid.Edit(self._textPrompt, initial_text)
self._widgetLinesList = urwid.SimpleFocusListWalker([])
self._widgetBufferListBox = urwid.ListBox(self._widgetLinesList)
self._w = urwid.Frame(header=self._widgetHeader,
body=self._widgetBufferListBox,
footer=self._widgetPromptText,
focus_part="footer")
Ok, now the problem is that when my terminal window loses focus, and I click on it again, by clicking the title bar of the xterm window, OR by directly clicking the bottom urwid.Edit that acts as the user's input for commands, everything is fine.
BUT, if I click on the screen of the xterm window, the bottom urwid.Edit loses focus, so it also loses the cursor. I have to click again on the urwid.Edit to get the cursor appearing again, and be able to write.
It seems that when with the mouse I click the screen, urwid leave focus from the bottom urwid.Edit and gives it to the urwid.SimpleFocusListWalker or the urwid.ListBox, without the possibility of giving back focus to the urwid.Edit, unless I click with the mouse on it.
I definitely don't want this!
How I can tell urwid to not give focus to the urwid.SimpleFocusListWalker or the urwid.ListBox, or simply to give focus to the urwid.Edit when one of them gets it?
More importantly, I wish to be possible to tell urwid to never leave focus from the urwid.Edit I use to write commands.
Any help?!
Uhm, it seems I have been able to solve the error by adding this code to my class:
def mouse_event(self, size, event, button, col, row, focus):
pass
Now, when I click on the surface of the terminal, the cursor doesn't disappear anymore.
That's exactly what I was looking for.

Have a popupmenu appear on a popupmenu on right-click

I have a wx Popupmenu appear when I left-click on a toolbar LabelTool. I already have a binding to have a handler run when I left click an item in the menu, but I want another popup menu to appear when I right click an item in the original popup menu. I've already tried binding it to wx.EVT_RIGHT_DOWN but it doesn't do it. Whenever I try right clicking on a menu object, it seems it still calls the wx.EVT_MENU, which calls the same handler as a left-click would. How would I implement this?
As far as I can tell, that is not supported. Besides, no user is going to expect that they need to right click on a context menu anyway. You should rethink your design so it's more intuitive. Perhaps by using a sub-menu instead of a secondary popup menu

Python/Tkinter: drawing an Entry on top of a Button

I have a Python/Tkinter app with a bunch of Buttons and I want to give the user an ability to edit the text on these buttons.
The most intuitive approach to me is to create an Entry widget on top of the button if the user right-clicks the button, and temporarily hide the button.
I did that and it's somewhat working but there is a problem. The button does not want to stay hidden. E.g. I create an Entry widget and it initially appears on top of the Button. Then, if I click inside the entry box, it briefly disappears (for a second or two), showing the button again, and then reappears. If I hit 'tab' and move focus out of the Entry, it disappears completely (exposing the button again) and only reappears when I click back into the entry/button area.
Am I doing something wrong / unintended, is that a bug, and is there a workaround?
Here's my Entry creation code ('self' is the button widget):
def on_mouse_rightclick(self, event):
self.prev_state = self["state"]
self.entry = Entry(self, {"width":8})
self.entry.pack(side=TOP, anchor=W)
self.entry.insert(0, self['text'])
self.entry.bind('<Return>', self.on_entry_edit)
self.entry.bind('<Escape>', self.on_escape)
self['state'] = DISABLED
self.lower(self.entry)
I tried to call self.entry.lift() and even to unbind '<Button-1>' temporarily from the button widget, with no effect.
I'm using 32-bit Python 2.7.9 on Windows.

Categories