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.
windows 10 python 3.6.10 wxPython 4.1.0
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 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
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