wxPython: Eliminate DC background flicker while moving shapes on a bitmap - python

I'm trying to create a panel on wxPython with a user-specified bitmap on the background, where a number of shapes can be dragged.
The expected behaviour is:
User selects an image file on an open file dialog before the panel is initialised;
The image becomes the background of the panel and is scaled to fit the panel while keeping an aspect ratio that depends on a previous user input;
A few circles appear over the image and can be dragged by the user.
I've been able to implement this with no functional problems, but I've been having some trouble with background flicker and so far, the solution I've found that results in the smallest amount of flickering is:
Create a BufferedDC from the loaded image when the panel is created;
Create a PaintDC inside the EVT_PAINT handler;
StretchBlit the BufferedDC into the PaintDC;
Draw the circles on the PaintDC;
Refresh the panel on any event that changes the circles' position or visibility.
Since the circles are draggable, one of these events is mouse motion, so the panel is refreshed every time the mouse moves over the panel, causing flickering.
How can I implement this behaviour in a way that eliminates background flicker?

Related

How to change property of a certain area on a widget?

Say I have an opaque window for the user to click and drag on, creating a selection rectangle. Is it possible to make just that rectangular area transparent, leaving the rest of the window opaque? Kind of like this image. The black being the opaque part and the white being the transparent part. All part of the same widget.
Edit: Transparent as in being able to see behind the window.

resize all contents of window?

I'm making a game with Tkinter canvas, and I'm planning to expand it (for UI) using grid.
Problem I'm facing is, that as it is, game window takes up bit more than the height of my screen. If I add anything on top, some parts will be completely off-screen. I can not change resolution of the canvas, as some parts move on pixel-basis. Thus I want to change size of the whole 'image' that Tkinter renders(canvas + other possible labels), that is the whole window.
Is there a way to do so?
window snapshot for reference

Drawing a rubberband on a panel without redrawing everything in wxpython

I use a wx.PaintDC() to draw shapes on a panel. After drawing the shapes, when I left click and drag mouse, a rubberband (transparent rectangle) is drawn over shapes. While dragging the mouse, for each motion of mouse, an EVT_PAINT is sent and everything (all shapes and rectangle) is redrawn.
How do I just draw the rubberband over the existing shapes (I don't want to redraw the shapes), I mean, it would be nice if I can save the existing shapes on some DC object and just draw the rubberband on it. So that the application will draw it faster.
You presumably want to have a look at wx.Overlay. Look here for an example.

wxPython Slider background colour incomplete change

wxPython 2.8.10, Python 2.6.2, Windows 7 x64
So I am having a bit of a wxPython issue I am hoping someone can say where I am going wrong. In my application I have Sliders where the background colour changes as you move the slider around. I do this via SetBackgroundColour from within a EVT_SLIDER handler. It mostly works, but I end up with a border of unchanged colour around the outer edge of the widget.
I have tried Refresh up the chain, RefreshRect on the top level Frame around the widget (and a few pixels further out), various combinations of Update and Layout, but nothing seems to erase it. The best I have managed is if I use something like:
frame = wx.GetTopLevelParent(slider)
rect = slider.GetRect()
slider.ClearBackground()
slider.SetBackgroundColour(wx.Colour(red,green,blue))
frame.RefreshRect(rect)
It will correctly draw the background box with the new colour without the border issue, but it will not repaint the slider itself.
However, if I click away so the focus is elsewhere, the border snaps to the new colour.
I could probably use an overlay or custom paint function but I am hoping there is something I am missing here that will get me the desired results.
Any suggestions?
(edited to add)
Looks like resizing the window manually corrects the issue, but Layout() does not.
It also functions correctly if you do the client size trick:
w,h = slider.GetClientSize()
slider.SetClientSize( (w+1,h) )
slider.SetClientSize( (w,h) )

wxPython: Making a scrollable DC

I am drawing inside a wx.Window using a PaintDC. I am drawing circles and stuff like that into that window. Problem is, sometimes the circles go outside the scope of the window. I want a scrollbar to automatically appear whenever the drawing gets too big. What do I do?
Use a wx.ScrolledWindow and set the size of the window as soon as your 'drawing go outside' the window with
SetVirtualSize(width,height)
If this size is bigger than the client size, then wx will show scrollbars. When drawing in the window make sure to use CalcUnscrolledPosition and CalcScrolledPosition
Here you can find some more information.

Categories

Resources