Search code examples
pythonwxpython

How to create a frame in wxPython across multiple monitors?


I'm trying to create a frame in wxPython that extends across multiple screens. The setup in which I'm trying this consists of a monitor with 1366x768 plus another monitor in portrait mode with 1080x1920.

GetSystemMetrics(76) and GetSystemMetrics(77) from win32api result in 0 and -1144 to get the left top of the virtual screen area.

GetSystemMetrics(78) and GetSystemMetrics(79) give me the total virtual screen resolution of 2446x1920.

When I call the frame with pos = (GetSystemMetrics(76), GetSystemMetrics(77)) and size = (GetSystemMetrics(78), GetSystemMetrics(79)) for some reason it gives me a frame with the size of the first monitor only.

import wx
from win32api import GetSystemMetrics

class SelectableFrame(wx.Frame):

    c1 = None
    c2 = None

    def __init__(self, parent, id, title, pos, size):
        wx.Frame.__init__(self, parent, id, title, pos, size, style=wx.NO_BORDER) 
        self.Show(True)
        self.ToggleWindowStyle(wx.STAY_ON_TOP)
        self.SetFocus()
        self.Raise()
        print(pos)
        print(size)

        self.Bind(wx.EVT_MOTION, self.OnMouseMove)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self.SetCursor(wx.Cursor(wx.CURSOR_CROSS))

        self.alphaValue = 100
        self.SetTransparent(self.alphaValue)
        self.Maximize(True)

    def OnMouseMove(self, event):
        global x2, y2
        if event.Dragging() and event.LeftIsDown():
            self.c2 = event.GetPosition()
            x2 = self.c2.x
            y2 = self.c2.y            
            self.Refresh()

    def OnMouseDown(self, event):
        global x1, y1
        self.c1 = event.GetPosition()
        x1 = self.c1.x
        y1 = self.c1.y
        self.Refresh()

    def OnMouseUp(self, event):
        print(self.c1)
        print(self.c2)
        self.SetCursor(wx.Cursor(wx.CURSOR_ARROW))           
        self.Destroy()

    def OnPaint(self, event):
        if self.c1 is None or self.c2 is None: return

        bdc = wx.PaintDC(self)
        dc = wx.GCDC(bdc)
        dc.SetPen(wx.Pen('red', 1))
        dc.SetBrush(wx.Brush(wx.Colour(0, 0, 0), wx.TRANSPARENT))

        dc.DrawRectangle(self.c1.x, self.c1.y, self.c2.x - self.c1.x, self.c2.y - self.c1.y)

if __name__=="__main__":

    app=wx.App(redirect=False)
    selectionFrame2 = SelectableFrame(
            parent=None, 
            id=wx.ID_ANY, 
            title="", 
            pos=(GetSystemMetrics(76),GetSystemMetrics(77)), 
            size=(GetSystemMetrics(78), GetSystemMetrics(79))
            )
    selectionFrame2.Show(True)

    app.MainLoop()

How do I generate this frame in wxPython that extends across multiple monitors and set it up so that it works with any combination of monitors?


Solution

  • wx.Display.GetCount()
    # then you can get the geometry for each display
    d = wx.Display(0)
    d.GetGeometry()
    

    That should give you the real sizes and positions of the displays.