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

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.

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 disable right click on a pop-up menu

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)'

How do I bind '<Return>' to focused (tabbed) button in tkinter?

I have multiple buttons in my tkinter 8.5 GUI (on Windows 7). I want whatever button is focused on (tabbed over) to be selected when the user hits Enter. I know I have to bind '<Return>', but I need the rest of the gaps filled in.
Thanks in advance!
Assuming you want this to be universal to all applications in the root window you could do something similar to this.
def clickButton():
widget = root.focus_get()
if widget != root:
widget.invoke()
root = Tkinter.Tk()
root.bind("<Return>", clickButton)
root.mainloop()
That will run any command associated with the currently tabbed selection. If you want to limit it to certain buttons you can do type-checking inside of the method. Widget will be whatever widget is currently in focus via the tabbed selection. Also beware of a user hitting enter on certain widgets that may not support the invoke method.

Keep selection on Tkinter Entry widget despite not having focus

When a user selects a portion of text in a Tkinter Entry widget it becomes highlighted. However, when the user clicks away from the widget the highlighting disappears.
Is there any way to keep the selected text highlighted despite the Entry widget not having focus?
I'm attempting to make a custom right-click menu not based on the Tkinter Menu widget (it's based on a Tkinter Toplevel widget), and I would like the text to remain highlighted despite the menu having focus.
You want to set the exportselection option of the text widget to False
text_widget.configure(exportselection=False)

Keep PyGTK Button from Resizing on Label Change

I'm working on a PyGTK app with some Buttons that, when clicked, give a text entry dialog, then set the text on the button to whatever was entered in the box. The problem is that if the text is longer than the button can show, the button changes size to accomodate. How do I keep GTK Buttons from resizing when the text changes?
Have you tried set_size_request?
http://library.gnome.org/devel/pygtk/stable/class-gtkwidget.html#method-gtkwidget--set-size-request
button = gtk.Button("text on button")
button.set_size_request(width=30, height=20)
See:
http://www.pygtk.org/docs/pygtk/class-gtkwidget.html#method-gtkwidget--set-size-request
In glade, you can set "Width request" in the "Common" tab.

Categories