首页 > 解决方案 > 如何在 wxPython 中跨多个监视器创建框架?

问题描述

我正在尝试在 wxPython 中创建一个跨多个屏幕的框架。我正在尝试的设置包括一台 1366x768 的显示器和另一台 1080x1920 的纵向模式显示器。

Win32api 中的 GetSystemMetrics(76) 和 GetSystemMetrics(77) 得到 0 和 -1144 以获取虚拟屏幕区域的左上角。

GetSystemMetrics(78) 和 GetSystemMetrics(79) 给了我 2446x1920 的总虚拟屏幕分辨率。

当我出于某种原因使用 pos = (GetSystemMetrics(76), GetSystemMetrics(77)) 和 size = (GetSystemMetrics(78), GetSystemMetrics(79)) 调用框架时,它给了我一个只有第一个监视器大小的框架。

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()

如何在 wxPython 中生成这个跨多个监视器的框架并设置它以便它与任何监视器组合一起工作?

标签: pythonwxpython

解决方案


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

这应该为您提供显示器的真实尺寸和位置。


推荐阅读