Create a gadget-like transparent background image type window using wxpython

There is no gadget in windows10

I suddenly missed the gadget that was in windows vista etc., so I decided to make something close to it, so I made it using wxpython. Those who want to see only the final code are at the bottom.

Deliverables

apppleeeeeeee.gif

environment

windows 10 python 3.6.10 wxPython 4.1.0

Make the background transparent and display the image

Makes the background transparent and changes the window to the shape of the image. First, by setting NO_BORDER, FRAME_SHAPED in the initialization process, the title bar will be deleted and the SetShape method will be available.

wx.Frame.__init__(self, None, title="Apple!!",
        style=wx.NO_BORDER | wx.FRAME_SHAPED, pos=(110, 10))

afterwards Load any image, change the window size, and change the window shape. As a transparent image Since it could not be read, I decided to treat the color (0,0,0) as a transparent color.

image = wx.Image("apple.png ")
self.bitmap = image.ConvertToBitmap()
self.imageSize = image.GetSize()
self.SetClientSize(self.imageSize)
self.SetShape(wx.Region(self.bitmap, wx.Colour(0, 0, 0)))

Finally, bind the onPaint event to draw the image.

self.Bind(wx.EVT_PAINT, self.onPaint)

def onPaint(self, event=None):
    DC = wx.PaintDC(self)
    DC.DrawBitmap(self.bitmap, 0, 0, True)

The image type window is now displayed.

I can't move the window because there is no title bar !!

I want to be able to move it because it is a big deal. There seems to be a slightly easier way here, but I try to update the position by detecting the part that is being clicked and moving in the Move event. The important part is to convert the acquisition position to the absolute position of the screen.

def onEvent(self, event):
    pos = event.Position
    pos = self.ClientToScreen(pos)
    if self.leftDown and event.leftIsDown:
        self.position[0] += pos.x-self.x
        self.position[1] += pos.y-self.y
        self.Move(self.position)
    self.x = pos.x
    self.y = pos.y
    self.leftDown = event.leftIsDown

Final code

It has nothing to do with the code, but if you run it using pythonw, it will only bring up the window without displaying the console, so it is recommended.

import wx

class AppFrame(wx.Frame):
    def __init__(self):
        self.position = [100, 100]
        wx.Frame.__init__(self, None, title="Apple!!",
                          style=wx.NO_BORDER | wx.FRAME_SHAPED, pos=(110, 10))
        self.Bind(wx.EVT_MOUSE_EVENTS, self.onEvent)
        self.Bind(wx.EVT_PAINT, self.onPaint)
        self.Move(self.position)
        self.leftDown = False
        self.x = -1
        self.y = -1

        image = wx.Image("apple.png ")
        self.bitmap = image.ConvertToBitmap()
        self.imageSize = image.GetSize()
        self.SetClientSize(self.imageSize)
        self.SetShape(wx.Region(self.bitmap, wx.Colour(0, 0, 0)))

    def onEvent(self, event):
        pos = event.Position
        pos = self.ClientToScreen(pos)
        if self.leftDown and event.leftIsDown:
            self.position[0] += pos.x-self.x
            self.position[1] += pos.y-self.y
            self.Move(self.position)
        self.x = pos.x
        self.y = pos.y
        self.leftDown = event.leftIsDown

    def onPaint(self, event=None):
        DC = wx.PaintDC(self)
        DC.DrawBitmap(self.bitmap, 0, 0, True)


app = wx.App(False)
AppFrame().Show()
app.MainLoop()

Recommended Posts

Create a gadget-like transparent background image type window using wxpython
Create a frame with transparent background with tkinter [Python]
Create a GCE instance from a GCR Docker image using terraform
Create a simple scheduled batch using Docker's Python Image and parse-crontab
Generate a Docker image using Fabric
Create a python GUI using tkinter
Create a nested dictionary using defaultdict
Create a CRUD API using FastAPI
Create a C wrapper using Boost.Python
Create a dummy image with Python + PIL.
Create a graph using the Sympy module
[Python] Create a Batch environment using AWS-CDK
Create a dataframe from excel using pandas
[AWS Lambda] Create a deployment package using the Docker image of Amazon Linux