Keep selection on Tkinter Entry widget despite not having focus - python

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)

Related

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.

PyQt Button Selection

I have a PyQt GUI set up that has a selection of QPushButtons and a QLineEdit text box (among other things). The text box is set up so as to call a function upon returnPressed(). My problem is that when I click on the text box and put in text, one of the buttons becomes selected which means that when I press enter in the text box it activates both the button and the text box function.
Is there a way around this? Some way to stop any buttons from being selected while the text box is being edited?
The code is fairly long so I can't add it here but if there are any questions regarding layout or anything that may be relevant, please ask.
Thank you for any help you can offer
From your question and comments, I'm guessing that the buttons and line-edit are in a QDialog, and that the selection/highlighting occurs due to the default/autoDefault property of buttons.
Normally, these properties will be set to False, but in a QDialog they are automatically set to True. The button that is the current default gets an additional frame drawn around it (even when it doesn't have the keyboard focus), and is activated whenever the return key is pressed.
You can of course prevent this behaviour by simply doing:
button.setDefault(False)
button.setAutoDefault(False)
for each button in the dialog.

Is there a way for an entry widget to automatically be selected

I have a popup window with an entry widget using Tk, is there a way for the cursor to already be flashing when it pops up, so that the user doesn't have to click the entry widget to enter text?
You can use .focus()
widget_name.focus()

'hover over' popup with Tkinter

I have implemented an informational popup in a python app using a Tkinter Menu widget. I have a Text widget on a canvas in the root window. I created a Menu widget that has root as its parent. When I detect a mouse hover over the text widget I post the popup menu with menuWidget.post(). When I get a leave event from the text widget my intention was to have the popup disappear by calling menuWidget.unpost(), only the popup menu does not disappear until I click elsewhere outside the text widget.
First, is this a sane method for implementing an informational popup? And can anyone tell me why the popup menu won't disappear?
This is not the right way to do an informational popup. On the Mac and on windows machines menus are native controls. Because of this the unpost command doesn't work because tk cedes control to the system event loop in order to get platform-specific behavior.
What you want is to use instead is a toplevel window with the overrideredirect flag set. This lets you display a borderless window anywhere you want. The upside to this is that you aren't limited to simple text -- you can put anything you want in that toplevel -- another text widget, a canvas, buttons, etc.

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