how to disable the window close button in pygame? - python

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.

Related

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.

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.

Avoid event grab during motion in Tkinter

Is it possible in Tkinter to avoid the event grab which occures when you press a mouse button over a widget and keep it pressed while you move the mouse?
I want to register the mouse button and then track all widgets the user enters while he moves his mouse with the button pressed. When the user releases the mouse button the application executes the same action for all tracked widgets.
The following code should explain what I want to do.
# Set a tracking flag
widget.bind('<Button>', start_tracking)
# Add the entered widget to the tracked widgets, if the tracking flag is set
widget.bind('<Enter>', add_to_tracked_widgets)
# Execute an action for every tracked widget; unset the flag
widget.bind('<ButtonRelease>', end_tracking)
I took a look at the grab_current and grab_status methods, but they always returned None.
Python version is 3.4.1.
This is probably the most complicated way to do this, but okay.
One thing that makes this more complicated is Tkinter itself, because event.widget still refers to the widget that was clicked on initally. A different event we can use is Motion which is activated when the mouse moves inside a widget.
tk.bind("<Motion>", add_tracked)
I think you can implement the list and state variables yourself, so we come to the add_tracked method (I just renamed it, it's your add_to_tracked_widgets):
def add_tracked(event):
if tracking:
# Get coordinated of the event and use the master window method to determine
# wich widget lays inside these.
widget = tk.winfo_containing(event.x_root, event.y_root)
# Since 'Motion' creates many events repeatedly, you have to convert this
# list into a set to remove duplicates.
widgets.append(widget)

Wxpython closing windows

I have two windows in my application and one of these windows has no frame for design purposes.
The frame with the window has the usual toolbar with the minimize, restore, maximize and close buttons.
Is there a way i can close the window with no frame, with the framed windows close button?
Yes. You just need to save a reference to the 2nd frame. Something like this should suffice:
self.secondFrame = MySecondFrame()
Then in the first frame's close method, you can just do something like this:
self.secondFrame.Close()
However, I should note that creating a frame without the usual toolbar goes against most OS GUI guidelines and users will likely be irritated by that design decision.
EDIT: Yes, you can catch the event that occurs when the user presses the "X" button on the window via wx.EVT_CLOSE. When you do that, you need to call the main frame's Destroy() method instead of its Close method or you'll end up in an infinite loop since calling Close() fires EVT_CLOSE. You can still use Close() for the second frame though.

How to get clicks on disabled buttons with wxpython?

I have a disabled button, and it does not receive clicks when I use EVT_BUTTON on it. Is there a way to receive clicks even when it has been Disabled()?
The whole point of disabling a button is so that the EVT_BUTTON event is not fired. I'm sure you could create create an ugly hack using EVT_LEFT_DOWN and detecting where the mouse is in your app as a workaround, but why bother? This is intended behavior.
Perhaps wxpython has a mechanism similar to pygtk.
In pygtk you create a input-only (that is transparent) window over the widget you want to get clicks for and get your clicks there.

Categories