How to disable multiple auto-redrawing at resizing widgets in PyQt? - performance

I have a PyQt4 program with widgets whose content redraws very slowly (it's ok, because of my tasks). And when I trying to resize those widgets, program is trying to redraw a lot of times while mouse is not released. That's a lot of freezes.
I want to disable that auto-redrawing and configure PyQt to redraw all widgets only when mouse is released (which means that redraw happens exactly one time per one resize).
How to do that?
Edit1. I'll see it quite simply, like this: you drag the line, and while you dragging, all widgets stand. When you release it, widgets redrawing. But I'm really not sure that it's possible in PyQt4.

First, I would recommend making sure that if you are using custom paint events with your widgets, that you are not doing too heavy of work in each event and simply looking for a band-aid solution. If this is the case, try and find a way to cache or reduce the work. Otherwise...
The decision to draw opaque or not is one made by the window manager of your platform. As far as I know, there is not a simple attribute to toggle this feature. Something similar to this exists on a QSplitter to only draw after the handle is released.
I can offer one workaround approach, which is to delay the update until after no resize has occurred for a period of time. This will give your application some breathing room to reduce the paint events.
from PyQt4 import QtCore, QtGui
import sys
class DelayedUpdater(QtGui.QWidget):
def __init__(self):
super(DelayedUpdater, self).__init__()
self.layout = QtGui.QVBoxLayout(self)
self.label = QtGui.QLabel("Some Text")
self.layout.addWidget(self.label, QtCore.Qt.AlignCenter)
self.delayEnabled = False
self.delayTimeout = 100
self._resizeTimer = QtCore.QTimer(self)
self._resizeTimer.timeout.connect(self._delayedUpdate)
def resizeEvent(self, event):
if self.delayEnabled:
self._resizeTimer.start(self.delayTimeout)
self.setUpdatesEnabled(False)
super(DelayedUpdater, self).resizeEvent(event)
def _delayedUpdate(self):
print "Performing actual update"
self._resizeTimer.stop()
self.setUpdatesEnabled(True)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
win = QtGui.QMainWindow()
view = DelayedUpdater()
win.setCentralWidget(view)
win.show()
view.delayEnabled = True
app.exec_()
You will notice that as you resize the main window quickly, no updates are occurring for the custom widget, because we have turned them off in the resize event. A QTimer is trying to fire every 100 ms to perform the update and stop itself. But each time another resize event occurs, it will restart that timer. The effect is that timer will continue to be reset. leaving updates disabled, until a delay occurs.
Try holding down the mouse, resizing a little, wait, and resize some more. The update should occur even while your mouse is down but you are not resizing. Adjust the delay to suit. And you have control over turning the feature on and off with the bool flag.
This example could also be re-worked to make DelayedUpdater just a QObject, which accepts some QWidget instance as an argument. It would then set itself to be the eventFilter for that object and monitor its resizeEvent. That way you don't have to subclass normal widgets just to add this. You would simply make an instance of DelayedUpdater and use it as a utility object to monitor the widget.
Here is an example of making it a helper object:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.someWidget = QtGui.QWidget()
self.setCentralWidget(self.someWidget)
self.layout = QtGui.QVBoxLayout(self.someWidget)
self.label = QtGui.QLabel("Some Text")
self.layout.addWidget(self.label, QtCore.Qt.AlignCenter)
self.delayer = DelayedUpdater(self.someWidget)
class DelayedUpdater(QtCore.QObject):
def __init__(self, target, parent=None):
super(DelayedUpdater, self).__init__(parent)
self.target = target
target.installEventFilter(self)
self.delayEnabled = True
self.delayTimeout = 100
self._resizeTimer = QtCore.QTimer()
self._resizeTimer.timeout.connect(self._delayedUpdate)
def eventFilter(self, obj, event):
if self.delayEnabled and obj is self.target:
if event.type() == event.Resize:
self._resizeTimer.start(self.delayTimeout)
self.target.setUpdatesEnabled(False)
return False
def _delayedUpdate(self):
print "Performing actual update"
self._resizeTimer.stop()
self.target.setUpdatesEnabled(True)
Note that we are using this on just some arbitrary widget inside of our main window. We add a delay updater to it with this line:
self.delayer = DelayedUpdater(self.someWidget)
The DelayedUpdater watches the resize events of the target widget, and performs delayed updates. You could expand the eventFilter to also watch for other events, like a move.

Related

How do I add a small button to a ScatterLayout in Kivy? Label works auto, Button does not

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.scatterlayout import ScatterLayout
class ZoomWidget(ScatterLayout):
def __init__(self, **kwargs):
super(ZoomWidget, self).__init__(**kwargs)
self.add_widget(Label(text='Label'))
def on_touch_down(self, touch):
if touch.is_double_tap:
button = Button(text='Button')
self.add_widget(button)
return True
else:
return super().on_touch_down(touch)
class ZoomSpaceApp(App):
def build(self):
return ZoomWidget()
if __name__ == '__main__':
ZoomSpaceApp().run()
So, the label seems to work, I can right-click (on desktop OS) to create a second touch location, then left dragging scales & rotates the label. However, the button just seems to fill up the whole layout, and I can't scale or rotate it. So how do I get the Button to act as the Label does by default, within a ScatterLayout?
Also, I want to do something similar using other widgets, so the most general answer will do. For example, do I encapsulate the Button in some type of widget such as a layout? What shows up in the ZoomWidget will be other widgets. The point of my application is for the user to create their own apps by touches and decide what to put there (with a context menu at first). Knowing that, is there a better way to approach this?
It doesn't work, because Button works with touches too, therefore it'll most likely eat all your touches and won't let anything for ScatterLayout to work with. To get it work you need to have an area that belongs to ScatterLayout alone a user can work with.
As Label doesn't work with touches as Button does, it passes the touch to the first widget it can use it i.e. in this case the ScatterLayout.
def on_touch_down(self, touch):
if touch.is_double_tap:
button = Button(text='Button', size_hint=(0.9, 0.9))
self.add_widget(button)
return True
else:
return super(ZoomWidget, self).on_touch_down(touch)

How can I dynamically add actions to a QMenu while it is open on a Mac?

I have QSystemTrayIcon with a QMenu. In order to fill the menu, I need to fetch some things from the network, so I want to do that in the background.
So I have a QThread with a slot that is connected to the activated signal of the tray icon. Then the thread fetches the resources and updates the menu using another signal.
However, these updates do not show until I close and reopen the menu.
This seems to be a Mac specific problem. I ran my code on Windows, and there it updated more or less correctly. Is there any workaround?
Below is an extracted version of the problem. When the menu is opened, it will sleep 1 second in a thread and then change the menu. This change is not seen.
import sys
import time
from PySide import QtCore, QtGui
class PeerMenu(QtGui.QMenu):
def __init__(self):
QtGui.QMenu.__init__(self)
self.set_peers("prestine")
#QtCore.Slot(object)
def set_peers(self, label):
self.clear()
self.addAction(QtGui.QAction(label, self))
self.addSeparator()
self.addAction(QtGui.QAction("Hello", self))
class GUIListener(QtCore.QObject):
files = QtCore.Signal(object)
def __init__(self):
QtCore.QObject.__init__(self)
self.counter = 0
#QtCore.Slot()
def check(self):
time.sleep(1)
self.counter += 1
self.files.emit(str(self.counter))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
icon = QtGui.QSystemTrayIcon(QtGui.QIcon('images/glyphicons-206-electricity.png'), app)
listener = GUIListener()
t = QtCore.QThread()
t.start()
listener.moveToThread(t)
menu = PeerMenu()
icon.activated.connect(listener.check)
listener.files.connect(menu.set_peers)
icon.setContextMenu(menu)
icon.show()
app.exec_()
After a few hours of extensive googling I finally figured it out.
You can create a borderless window using QtGui.QMainWindow(parent=None, flags=QtCore.Qt.Popup) and then find the location of the icon with icon.geometry().center() and finally move the window there with window.move(icon_point).
There is some hackery involved in deciding how to place the window relative to the icon. Full code can be found at https://github.com/pepijndevos/gierzwaluw/blob/master/gui.py

Continuous calls of the <Configure> event in tkinter

I am trying to write a function to dynamically resize an image displayed in a tkinter window.
Therefore I bound this function to the Configure event:
connroot.bind( "<Configure>", connresiz)
My problems are:
That the connresiz() function gets called 3 times (why 3?) at program start, and
More troublesome, that dynamically resizing the window calls the function continuously as I drag the mouse! How can avoid this?
I thought about checking at the simultaneous presence of a <Configure> and <ButtonRelease-1> events, but I don't know how to code it.
1) We don't know that, since we can't see your code...
2) Short answer is: you can't, because that's exactly what <Configure> event does! Long answer, you can, with a little trick/hack. Since anytime the window is changing, it will call all the binded functions to <Configure>, and the same happens anytime as the mouse button released (right after the last <Configure> call) we can create a flag/switch which will tell us, if the window was "configured" then we can check that switch anytime the mouse button is released, and switch it back to the default value after we ran some actions.
So if you want the image to resized only, when the mouse was released and the window was changed this is the code you need:
from tkinter import *
class Run:
def __init__(self):
self.root = Tk()
self.clicked = False
self.root.bind('<ButtonRelease-1>', self.image_resize)
self.root.bind('<Configure>', lambda e: self.click(True))
def image_resize(self, event):
if self.clicked:
print("I'm printed after <Configure>.") # the action goes here!
self.click(False)
def click(self, value):
self.clicked = value
app = Run()
app.root.mainloop()
According to the official tk documentation, <Configure> events fire "whenever its size, position, or border width changes, and sometimes when it has changed position in the stacking order." This can happen several times during startup.
It is called continuously while you resize the window because the size of the widget is changing. That's what it's defined to do. You can't prevent it from being called, though you can certainly modify what you do in the callback. For example, you could delay resizing the image until you've not received another <Configure> event for a second or two -- which likely means the user has stopped interactive resizing.

wxPython Mouse Entering/Leaving event methods don't work on Linux

I am trying to provide a custom button class that needs a hover for it to work properly. I am therefore using StaticBitmap and extending it. But I am finding differences between Windows and Linux using the same exact code between both. The following small simple example doesn't work properly in Linux, but works fine in Windows:
import wx
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test")
panel = wx.Panel(self, wx.ID_ANY, size=(200,100))
panel.SetBackgroundColour("Black")
# create a normal bitmap button
bitmap = wx.Bitmap("images/power.png", wx.BITMAP_TYPE_ANY)
self.image1 = wx.StaticBitmap(panel, bitmap=bitmap, size=bitmap.GetSize())
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.image1)
panel.SetSizer(sizer)
# This works on Windows, but not on Linux
#self.image1.Bind(wx.EVT_ENTER_WINDOW, self.OnHover)
#self.image1.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveHover)
# This (used by itself) gets events for both Linux/Win, but
# doesn't find e.Entering() or e.Leaving() on Linux!
self.image1.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
def OnHover(self, e):
print "Hover"
def OnLeaveHover(self,e):
print "Leaving Hover"
def OnMouseEvents(self,e):
print "Mouse event"
#print dir(e)
if e.Entering():
print "Hover"
elif e.Leaving():
print "Leaving Hover"
else:
e.Skip()
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
Is this a known issue? Is there some other way to bind the event to get the mouseover/hover operation to work properly in Linux?
Also, no forms of self.Bind() work here on Linux or Windows. Why? i.e.:
self.Bind(wx.EVT_ENTER_WINDOW, self.OnHover, self.image1)
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveHover, self.image1)
Hmmm..., all I can tell you is what I experienced with your code when using it in Ubuntu 12.04
1) My picture never shows up (i.e. a grey window nothing more)
2) when binding the EVT_ENTER/LEAVE_WINDOW to self and not to image1 (see below), it works
# This now works on Linux
#self.Bind(wx.EVT_ENTER_WINDOW, self.OnHover)
#self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveHover)
I hope this helps you in some form. Also I would like to add that I have made many a bad experience with cross-platform compatibility of wxpython unfortunately. I like the library a lot, but once it delves into more obscure widgets, the behaviour may differ wildly between Windows and Linux.
I remember trying to use the MPlayerCtrl once and for the same method in Windows it would return 0 while in Linux it would return -1 (can't mind exactly which method it was, that project got dumped once I noticed it)
Maybe, this is another one of those strange behaviours...
Having thought about it some more.
instead of binding the event to the picture, you could bind it to the widget it is displayed in, such as your panel.
self.panel = wx.Panel(self, wx.ID_ANY, size=(200,100))
self.panel.SetBackgroundColour("Black")
...
#self.panel.Bind(wx.EVT_ENTER_WINDOW, self.OnHover)
#self.pane.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveHover)
you should bind the wx.EVT_ENTER_WINDOW/wx.EVT_LEAVE_WINDOW event to self.panel. that works.
wx.EVT_ENTER_WINDOW/wx.EVT_LEAVE_WINDOW is not the subclass of wx.CommandEvent class. It isn't thrown up to the parent window to process.

Create a PyGTK GUI with event to reload itself

I'm prototyping GUI layout with PyGTK, sometimes using glade/builder sometimes not, and the following scene repeats endlessly:
Modify the code
Run the GUI script
Judge the result
Modify again
Run again...
So, since I heard that Python allows reloading of modules, I would like to know if it is possible to modify the code WITHOUT CLOSING THE WINDOW, and then, from the window itself, say, clicking on a button, "reload" the window reflecting the changes in code.
Since it is a conceptual question, I don't have any specific code to show.
Thanks for the attention
I think it is possible if you do the following:
Identify and isolate the widget W that you want to see updated when you press the button (if you want to see the whole window updated, then make it whatever you add in the window, not the window itself).
Write a function (or class) that creates and returns this widget
Put this function or class in a module that you will reload
Create your button outside W and connect it to a function that does the following
Remove current W from window
Reload the module
Create new instance of W
Add it to the window
Of course, the critical step here is "reload the module". I guess you have to make sure no code from the module is running and no other module depends on variables defined on this module.
EDIT: I had some time, so I made a little prototype. Change the label in widget_module.py and then hit Update
gui.py
# Load in pygtk and gtk
import pygtk
pygtk.require('2.0')
import gtk
import widget_module
# Define the main window
class Whc:
def __init__(self):
# Window and framework
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.destroy)
# A Button, with an action
# Add it to the geometry
# show the button
self.button = gtk.Button("Update")
self.button.connect("clicked", self.update, None)
self.vbox = gtk.VBox()
self.vbox.pack_start(self.button)
self.widget = widget_module.get_widget()
self.vbox.pack_start(self.widget)
self.window.add(self.vbox)
# Show the window
self.window.show_all()
# Callback function for use when the button is pressed
def update(self, widget, data=None):
print "Update"
self.vbox.remove(self.widget)
reload(widget_module)
self.widget = widget_module.get_widget()
self.vbox.pack_start(self.widget)
self.widget.show()
# Destroy method causes appliaction to exit
# when main window closed
def destroy(self, widget, data=None):
gtk.main_quit()
# All PyGTK applicatons need a main method - event loop
def main(self):
gtk.main()
if __name__ == "__main__":
base = Whc()
base.main()
widget_module.py
import pygtk
import gtk
def get_widget():
return gtk.Label("hello")

Resources