wx.DrawRectangle上的wxPython弹出窗口/工具提示?

时间:2019-04-30 02:57:43

标签: popup wxpython

在下面的代码中,DrawRect1和DrawRect2表示在屏幕上绘制多个形状的函数的简化版本。

如果我将鼠标悬停在任何绘制的矩形上(类似于工具提示的工作方式),我想显示一些补充信息。但是我需要从函数而不是静态定义中生成显示信息。

鉴于我知道绘制矩形的坐标,我可以创建具有相同坐标的另一种类型的对象,还是将悬停动作链接到每个drawedrectangle,这样我就可以调用定义了这样的函数? :

编辑:我想我需要一个可以将wx.EVT_ENTER_WINDOW事件绑定到的对象,可以与dc.DrawRectangle同时创建?还是可以将此处理程序绑定到面板并使用x,y位置尝试与绘制的矩形坐标列表匹配?

我在SO上能找到的最接近的问题是这个古老的问题wxpython tooltip at specific coordinates,但这不是一个全面的答案。

import wx


class Mywin(wx.Frame):

    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title=title, size=(500, 300))

        self.InitUI()

    def InitUI(self):

        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour(wx.Colour('RED'))


        self.Centre()
        self.Show(True)

        menuBar = wx.MenuBar()
        RectangleButton = wx.Menu()

        Item1 = RectangleButton.Append(wx.ID_ANY, 'Rectangle 1')
        Item2 = RectangleButton.Append(wx.ID_ANY, 'Rectangle 2')

        menuBar.Append(RectangleButton, 'Rectangles')

        self.SetMenuBar(menuBar)

        self.Bind(wx.EVT_MENU, self.DrawRect1, Item1)
        self.Bind(wx.EVT_MENU, self.DrawRect2, Item2)


    def DrawRect1(self, e):
        self.panel.SetBackgroundColour(wx.Colour('BLUE'))
        self.Refresh()
        self.Update()
        self.dc = wx.ClientDC(self.panel)
        self.dc.SetBrush(wx.Brush(wx.Colour('white')))
        self.dc.DrawRectangle(10, 10, 100, 100)

    def DrawRect2(self, e):
        self.panel.SetBackgroundColour(wx.Colour('GREEN'))
        self.Refresh()
        self.Update()        
        self.dc = wx.ClientDC(self.panel)
        self.dc.SetBrush(wx.Brush(wx.Colour('white')))
        self.dc.DrawRectangle(20, 20, 50, 50)

myApp = wx.App()
Mywin(None,'Drawing demo')
myApp.MainLoop()

1 个答案:

答案 0 :(得分:0)

据我从文档中得知,DC似乎不支持添加工具提示,这很烦人,特别是当基础wx小部件出现时。 目前,我能想到的最好的办法是跟踪鼠标的移动,但效果并不理想,我怀疑它的局限性太大,以至于没有任何帮助。
在做出这些警告之后,我为ToolTip之类的显示提供了3个选项。即,设置和取消设置面板本身的工具提示,状态栏条目和弹出窗口。
丢弃不需要的任何内容。

import wx

class Mywin(wx.Frame):

    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title=title, size=(500, 300))
        self.tips = ["","Rectangle 1","Rectangle 2"]
        self.rect = []
        self.InitUI()

    def InitUI(self):
        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour(wx.Colour('RED'))
        self.Centre()
        self.Show(True)

        menuBar = wx.MenuBar()
        RectangleButton = wx.Menu()
        self.status = self.CreateStatusBar()
        self.status.SetFieldsCount(number=2)
        Item1 = RectangleButton.Append(wx.ID_ANY, 'Rectangle 1')
        Item2 = RectangleButton.Append(wx.ID_ANY, 'Rectangle 2')

        menuBar.Append(RectangleButton, 'Rectangles')

        self.SetMenuBar(menuBar)

        self.Bind(wx.EVT_MENU, self.DrawRect1, Item1)
        self.Bind(wx.EVT_MENU, self.DrawRect2, Item2)
        self.panel.Bind(wx.EVT_MOTION, self.MouseMovement)


    def DrawRect1(self, e):
        self.panel.SetBackgroundColour(wx.Colour('BLUE'))
        self.Update()
        self.dc = wx.ClientDC(self.panel)
        self.dc.SetBrush(wx.Brush(wx.Colour('white')))
        self.dc.DrawRectangle(10, 20, 100, 200)
        #Note the position of the DC
        self.rect = [x for x in self.dc.BoundingBox]
        #Append the id
        self.rect.append(1)

    def DrawRect2(self, e):
        self.panel.SetBackgroundColour(wx.Colour('GREEN'))
        self.Update()
        self.dc = wx.ClientDC(self.panel)
        self.dc.SetBrush(wx.Brush(wx.Colour('white')))
        self.dc.DrawRectangle(20, 20, 50, 50)
        self.rect = [x for x in self.dc.BoundingBox]
        self.rect.append(2)

    def MouseMovement(self, event):
        x,y = event.GetPosition()
        self.panel.SetToolTip('')
        self.status.SetStatusText('', 1)
        if self.rect:
            if x >= self.rect[0] and x <= self.rect[2] and y >= self.rect[1] and y <= self.rect[3]:
                self.panel.SetToolTip(self.tips[self.rect[4]])
                self.status.SetStatusText("Hovering over "+self.tips[self.rect[4]], 1)
                win = Popup(self,self.rect[4],self.tips[self.rect[4]])
                pos = self.GetScreenPosition()
                win.Position(pos,(-1,-1))
                win.Popup()

class Popup(wx.PopupTransientWindow):

    def __init__(self, parent, id, id_text):
        wx.PopupTransientWindow.__init__(self, parent)
        panel = wx.Panel(self)
        panel.SetBackgroundColour("gold")

        text = wx.StaticText(panel, -1,
                          "This is a wx.PopupTransientWindow\n"
                          "Click mouse outside of it\n\n"
                          "Id of widget is "+str(id)+"\n"
                          "You are hovering over "+id_text)
        # add other widgets here if required
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(text, 0, wx.ALL, 5)
        panel.SetSizer(sizer)
        sizer.Fit(panel)
        sizer.Fit(self)
        self.Layout()



myApp = wx.App()
Mywin(None,'Drawing demo')
myApp.MainLoop()

enter image description here