How to keep an urwid.Edit always in focus? - python

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.

Related

Binding a key to a tkinter window, with exception of the entry field

Picture of my GUI for clarity
I made a tool that plays audio from youtube. I want it to pause when the spacebar is pressed. I do this by this piece of code:
win.bind('<space>',lambda event:funcPP(player, btnPP))
win is my window, funcPP is the play/pause function.
My problem is that I have also an Entry in my window (for searching videos) and of course it is impractical if the video pauses everytime when I search a new video and press Spacebar. And the real problem is that, once I have clicked the Entry field, the focus stays there. It doesn't go away after clicking somewhere else! This sabotages my Spacebar shortcut.
Any tips how I can solve this?
I found the solution.
With:
win.bind_all("<Button-1>", lambda event: event.widget.focus_set())
It is possible to put the focus whereever one clicks.
then:
win.bind('<space>',lambda event:funcPPTRANSFER(player))
To bind spacebar to the whole window
then:
def funcPPTRANSFER(player):
if str(win.focus_get()) != str(".!entry"):
funcPP(player)
Have the function check whether the spacebar was sent from the entry or from somwhere else in the window
EDIT: Another thing I found now:
https://stackoverflow.com/a/56519799/18664063
if isinstance(event.widget,tk.Tk): #check if event widget is Tk root window
very nice function that might help as well in this project.

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

Is it possible to see if the file is being dragged over the window in Kivy, Python?

What I want to achieve:
I want to detect the situation when the user is dragging a file over the Kivy app window.
What I already know:
I know how to detect hovering mouse coursor over widgets (with on_mouse_pos), I also know how to detect if a file is dropped onto the window (with on_file_drop).
So, is it possible to see whether the cursor is hovering over the window and "holding" a file? Because then I want to display some prompt (eg. 'Drop HERE'). I hope you get the idea :)
I'm not really sure, because there's this thing with SDL2 (and probably even with old pygame) when the Window just pauses (try some animation or something) when you e.g. drag it with the window decoration (the thing where title and _ O X are). That is the behavior if you do something with the Window directly.
Although, the Window looks like it behaves normally (doesn't pause itself), when you drag file on top of it (I tried with examples/animation/animate.py), to do such thing you'd need to do either the hovering behavior + handling the collisions or bind to mouse_pos.
However, when binding to mouse_pos, it seems like the Window still isn't capable of handling the input from outside and at the same time get mouse properties correctly (I think it's similar to the behavior when you click & drag outside of the Window and Button remains pressed, but this is kind of inversed).
edited animate.py:
class TestApp(App):
def on_mouse_pos(self, win, args):
print args
...
def build(self):
...
from kivy.core.window import Window
Window.bind(mouse_pos=self.on_mouse_pos)
return button
Therefore if you can't get even mouse position when a mouse button is being held, I don't think such an action is possible. You can however make the areas where you want to drop the file already different (e.g. change background) when you'll expect a user to drop the file - a very dirty workaround from UI side for such a problem.
Side note: Kivy should be able to get most (if not all) SDL2 window events via Cython, therefore if you find such event in SDL2 that would make fetching mouse position possible, such action could be performed, feel free to make a feature request in kivy/kivy or make a pull request.

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.

how to disable the window close button in pygame?

In a pygame application window, the minimize, resize and close buttons are present. Is there a way to disable the close(X) button?
I don't think there is, because some window managers don't give you the ability to remove the close button. But you can write an event handler such that the close button does whatever you want, including nothing.
Why do you want to prevent the user from closing? If it's just a matter that you would rather provide an in-game "quit" button that confirms and/or saves before quitting, you can perform the same task when the user hits the close button.
Just for the record, another option would be to pass the following argument to the set_mode() method call:
pygame.display.set_mode(..., flags = pygame.NOFRAME)
This however makes the whole frame go away, including the top strip to move the window around and the other buttons, such as minimize, so it's rather overkill for just getting rid of the X button.

Categories