motion-notify-event not triggering - python

Basically what i need to achieve with this is to bypass the bug which doesn't identify the mouse over the top bar as being inside the window. As you can see with the code below, if you put the mouse on top bar the window turns half-transparent as if the mouse aren't there (mouse_enter() doesn't get called).
Well, so i thought the only way to do this is to get mouse coordenates/position and work from there in the mouse_move() function, the problem now is that motion-notify-event doesn't get fired.
import gtk
def mouse_move(window, event):
print(win.get_pointer())
win.set_opacity(1)
def mouse_enter(window, event):
win.set_opacity(1)
def mouse_leave(window, event):
win.set_opacity(0.5)
win = gtk.Window()
win.set_opacity(0.5)
win.set_size_request(600, 400)
win.connect('enter-notify-event', mouse_enter)
win.connect('leave-notify-event', mouse_leave)
win.connect('motion-notify-event', mouse_move)
win.connect('destroy', gtk.main_quit)
win.show_all()
gtk.main()

You likely need to call win.add_events() with the Python equivalent of GDK_POINTER_MOTION_MASK.

Related

PyQt5: how to open one window in each connected screen?

I'm trying to create a screenshot utility for linux using python. Right now I'm stuck at trying to implement a function that lets the user select a region from a live screen and screenshot it. After much pondering, I reached the conclusion to create a full-screen window on each screen to get the mouse's click and drag coordinates.
How can I have my program create a full-screen window (without the toolbar icon) for each screen connected to the system?
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc
class InvisWindow(qtw.QWidget):
def __init__(self, screens):
super().__init__()
self.setWindowFlags(qtc.Qt.Tool | qtc.Qt.FramelessWindowHint)
self.show()
self.showFullScreen()
self.windowHandle().setScreen(screens[0])
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
mw = InvisWindow(app.screens())
sys.exit(app.exec_())
I found this code searching for a way to do it, but no matter which screen I pass to setScreen() it always appears on a single screen, i.e. changing the argument doesn't change which screen it appears in.
There are two problems:
as the documentation explains:
If the screen is part of a virtual desktop of multiple screens, the window will not move automatically to newScreen.
on Linux, there's some amount of time and system events between the call to show and when the window is actually mapped on the screen the first time (see Initial Geometry), which can be overridden by the window manager if no geometry is explicitly set;
That said, there should be no need to use the QWindow for this, as using move is usually be enough, you only must do it before any call to show() or related functions:
class InvisWindow(qtw.QWidget):
def __init__(self, screens):
super().__init__()
self.setWindowFlags(qtc.Qt.Tool | qtc.Qt.FramelessWindowHint)
self.move(screens[0].geometry().topLeft())
self.showFullScreen()
Note that there's no use in calling show() before showFullScreen(), since it implicitly calls setVisible(True).
If what you want is to show a single window on top of everything, then you could try the following:
class InvisWindow(qtw.QWidget):
mapped = False
def __init__(self, screens):
super().__init__()
self.setWindowFlags(
qtc.Qt.WindowStaysOnTopHint |
qtc.Qt.Tool |
qtc.Qt.FramelessWindowHint
)
self.show()
def moveEvent(self, event):
if not self.mapped:
geometry = qtc.QRect()
for screen in qtw.QApplication.screens():
geometry |= screen.geometry()
if self.pos() != geometry.topLeft():
self.setGeometry(geometry)
self.mapped = True
Please consider the last lines, as they are very important, because trying to do geometry changes in a geometry change event (moveEvent and resizeEvent) can cause recursion.

How to sound a bell when the user clicks outside a modal window?

The situation is simple. I have a main window with a Help - About menu.
When this menu item is clicked, a modal window is opened (let's say it's an About-window).
With self.grab_set() I disabled the main-window (although the modal window does flicker when you click the main title bar).
So far, so good.
Here is the question: I really like to sound a bell when the user clicks outside the modal window on the main window.
This is what I could find about grab_set(), really not that much:
[effbot] ...a method called grab_set, which makes sure that no mouse or keyboard
events are sent to the wrong window.
[effbot] Routes all events for this application to this widget.
[kite.com] A grab directs all events to this and descendant widgets in the application.
[google books] grab_set() ensures that all of the application's events are sent to w until a corresponding call is made to grab_release ([Me:] or till the window is destroyed?)
I'm not quite sure how to understand this: does it mean you can handle an event on the main window within the modal window (like sounding my bell)?
So I tried things like:
self.bind('<Button-1>', self.bell) Exception in Tkinter callback: _tkinter.TclError: bad window path name
parent.bind('<Button-1>', self.bell) Nothing happens
So, how to sound a bell like when clicked outside the modal window on the main window, like in so many other applications?
Derived questions:
Is it still possible to cature events from the main window after using
grab_set for the modal window?
Is there a way to prevent the flickering?
I really like to understand this mysterious grab_set() method.
Stripped code:
import tkinter as tk
class About(tk.Toplevel):
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
self.geometry('200x150')
#--- OK button
btn_ok = tk.Button(self, text='OK', command=self.destroy) # destroy with OK
btn_ok.pack(side=tk.TOP)
btn_ok.focus() # destroy with spacebar
#--- Make window modal
self.grab_set()
# self.wait_window() # is this necessary in this case?
# self.bind('<Button-1>', self.bell) ??? The question
class MenuBar(tk.Menu):
def __init__(self, parent):
tk.Menu.__init__(self)
helpmenu = tk.Menu(self, tearoff=0)
helpmenu.add_command(label='About', command=lambda: About(parent))
self.add_cascade(label='Help', menu=helpmenu)
class MainApp():
def __init__(self, parent):
parent.configure(background='#000000')
parent.geometry('800x600')
menubar = MenuBar(parent)
parent.configure(menu=menubar)
if __name__ == '__main__':
root = tk.Tk()
MainApp(root)
root.mainloop()
When you set a grab, all button clicks will go to the window with the grab. You capture them the way you capture any other event. In the case of a button click you do that by binding a function to <1>.
It's important to know that a binding on a root window or a Toplevel window will apply to all widgets in that window. For example, binding to self in your code will fire even when you click on the "Ok" button. Therefore, the callback should probably only do work when the widget associated with the event is the same as the toplevel.
Example:
class About(tk.Toplevel):
def __init__(self, parent):
...
self.bind("<1>", self.capture_click)
...
def capture_click(self, event):
if event.widget == self:
<your logic here>
In the case of wanting to know if the user clicked outside the window, you can use the coordinates of the event object to compare against the window to see if the click is inside or outside.
def on_click(self, event):
if event.widget == self:
if (event.x < 0 or event.x > self.winfo_width() or
event.y < 0 or event.y > self.winfo_height()):
self.bell()
I found a second solution. Though my question was explicitly about using grab_set(), this method does the same for me: making the window as modal as possible and sound a bell.
Instead of using self.grab(), you can also disable the parent window:
parent.attributes('-disabled', True)
Of course it needs to be enabled again when the OK button is clicked (and when the windows is closed with the [X] close control. However, my original About-window has no window decoration). The command for the OK-button becomes:
btn_ok = tk.Button(self, text='OK', command=lambda: self.closeme(parent))
...which calls the closeme function:
def closeme(self, parent):
parent.attributes('-disabled', False)
self.destroy()
The bell sounds automatically when clicking a disabled window.
Method 1: Keeps you in full control of the main window but does not 'freeze' the main window: you can still move it around.
Method 2: Completely freezes the main window, but if it happens to be (partially) covered by another window (not of this application), then you can only bring back to top using Alt+Tab (windows).
I'm sure I will use both techniques in the future depending on my needs.

Pyqt: Making a widget update without mouse focus

I'm making something akin to a screen recorder using the PyQT library. My problem is that the only way I can think to get the recording part of the application to run is in the "paint event" part of the widget class. Here's some code for example:
class MainWindow(QWidget):
def __init__(self):
#setup window
def initUI(self):
#init UI stuff
def paintEvent(self, event):
#capture the screen and then display it on this widget
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
My main problem is on the paintevent area. I could start a thread and let the at capture and save frames, but I want to actively display each frame on the window. This can work while the widget has focus, but once the mouse moves away, and the window loses focus, then it stops because the paintevent is not being activated.
Is there anyway to solve this? Thank you!

Gtk+ 3 make GtkTreeView render columns horizontally

I'm using Gtk+ 3 in a Python program I'm working on and I need the "columns" in a GtkTreeView to be rendered in horizontally instead of the vertical way they are now.
As you can see from this GIF, because the attachment names stack up, it expands the TreeView upwards, not what I want.
I'm using Glade to design the GUI and I can't find any options for this, nor can I find any mention of it in the Gtk+ 3 Docs.
Is this possible, or am I going to have to figure out how to hack a solution with multiple columns?
I want it to end up looking something like this:
If you use a Gtk.FlowBox with Gtk.Labels, you can do it like this. However, there is probably a better way than packing every Gtk.Label in a Gtk.EventBox.
from gi.repository import Gtk, Gdk
import string
class Window(Gtk.Window):
def __init__(self):
super().__init__()
self.connect('delete-event', Gtk.main_quit)
self.set_size_request(200, 150)
flowbox = Gtk.FlowBox()
self.add(flowbox)
for x in string.ascii_lowercase:
eventbox = Gtk.EventBox()
eventbox.add(Gtk.Label(label=x))
flowbox.add(eventbox)
eventbox.connect('button-press-event', self.on_button_press)
def on_button_press(self, widget, event):
if event.button == 3:
print('Right click on: ' + widget.get_child().get_text())
win = Window()
win.show_all()
Gtk.main()

How do I get the size of a pygtk window?

I'm trying to use gtk.window.get_size(), but it always just returns the default width and height. The documentation says
The get_size() method returns a tuple containing the current width and
height of the window. If the window is not on-screen, it returns the
size PyGTK will suggest to the window manager for the initial window
size. The size obtained by the get_size() method is the last size
received in a configure event, that is, PyGTK uses its locally-stored
size, rather than querying the X server for the size. As a result, if
you call the resize() method then immediately call the get_size()
method, the size won't have taken effect yet. After the window manager
processes the resize request, PyGTK receives notification that the
size has changed via a configure event, and the size of the window
gets updated.
I've tried resizing the window manually and waiting a minute or so, but I still get the default width and height.
I'm trying to use this to save the window size on quit so that I can restore it on start. Is there a better way to do this?
Here's the code snipit I have for my main quit.
def on_main_window_destroy(self, widget, data=None):
if self.view.current_view.layout == 'list':
self.view.current_view.set_column_order()
width = self.main_window.get_size()[0]
height = self.main_window.get_size()[1]
#test statement
print (width, height)
self.prefs.set_size_prefs(width, height)
self.prefs.set_view_prefs(self.view.current_view.media, self.view.current_view.layout)
gtk.main_quit()
I think I understand what's happening now. This is inside the destroy signal, so by the time the code gets called, the window is already gone. Is there a more canonical way of handling window resizing? I was hoping to avoid handling resize events everytime the user resized the window.
This seems to fix your problem:
import gtk
def print_size(widget, data=None):
print window.get_size()
def delete_event(widget, data=None):
print window.get_size()
return False
def destroy(widget, data=None):
gtk.main_quit()
window = gtk.Window()
window.connect('delete_event', delete_event)
window.connect('destroy', destroy)
button = gtk.Button(label='Print size')
button.connect('clicked', print_size)
window.add(button)
window.show_all()
gtk.main()
I think the key is calling get_size on the delete_event signal rather than the destroy signal. If you do it on the destroy signal, it's like you describe, it just returns the default size.
Try running:
while gtk.events_pending():
gtk.main_iteration_do(False)
right before calling window.get_size()

Categories