Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
绑定到wxButton的wxPython弹出窗口_Python_Wxpython_Popupwindow - Fatal编程技术网

绑定到wxButton的wxPython弹出窗口

绑定到wxButton的wxPython弹出窗口,python,wxpython,popupwindow,Python,Wxpython,Popupwindow,我四处寻找,浏览了一大堆不同的网站,但我找不到如何在wxPython中为wxButton创建弹出窗口 有什么想法吗?你看过wxPython的演示吗?它有几个使用wx.PopupWindow及其变体的示例。下面是一个基于演示的示例: import wx ######################################################################## class TestPopup(wx.PopupWindow): """""" #

我四处寻找,浏览了一大堆不同的网站,但我找不到如何在wxPython中为wxButton创建弹出窗口


有什么想法吗?

你看过wxPython的演示吗?它有几个使用wx.PopupWindow及其变体的示例。下面是一个基于演示的示例:

import wx

########################################################################
class TestPopup(wx.PopupWindow):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent, style):
        """Constructor"""
        wx.PopupWindow.__init__(self, parent, style)

        panel = wx.Panel(self)
        self.panel = panel
        panel.SetBackgroundColour("CADET BLUE")

        st = wx.StaticText(panel, -1,
                           "This is a special kind of top level\n"
                           "window that can be used for\n"
                           "popup menus, combobox popups\n"
                           "and such.\n\n"
                           "Try positioning the demo near\n"
                           "the bottom of the screen and \n"
                           "hit the button again.\n\n"
                           "In this demo this window can\n"
                           "be dragged with the left button\n"
                           "and closed with the right."
                           ,
                           pos=(10,10))
        sz = st.GetBestSize()
        self.SetSize( (sz.width+20, sz.height+20) )
        panel.SetSize( (sz.width+20, sz.height+20) )

        panel.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
        panel.Bind(wx.EVT_MOTION, self.OnMouseMotion)
        panel.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
        panel.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)

        st.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
        st.Bind(wx.EVT_MOTION, self.OnMouseMotion)
        st.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
        st.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)

        wx.CallAfter(self.Refresh)    

    def OnMouseLeftDown(self, evt):
        self.Refresh()
        self.ldPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
        self.wPos = self.ClientToScreen((0,0))
        self.panel.CaptureMouse()

    def OnMouseMotion(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            dPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
            nPos = (self.wPos.x + (dPos.x - self.ldPos.x),
                    self.wPos.y + (dPos.y - self.ldPos.y))
            self.Move(nPos)

    def OnMouseLeftUp(self, evt):
        if self.panel.HasCapture():
            self.panel.ReleaseMouse()

    def OnRightUp(self, evt):
        self.Show(False)
        self.Destroy()

########################################################################
class TestPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        btn = wx.Button(self, label="Open Popup")
        btn.Bind(wx.EVT_BUTTON, self.onShowPopup)


    #----------------------------------------------------------------------
    def onShowPopup(self, event):
        """"""
        win = TestPopup(self.GetTopLevelParent(), wx.SIMPLE_BORDER)

        btn = event.GetEventObject()
        pos = btn.ClientToScreen( (0,0) )
        sz =  btn.GetSize()
        win.Position(pos, (0, sz[1]))

        win.Show(True)

########################################################################
class TestFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test Popup")
        panel = TestPanel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = TestFrame()
    app.MainLoop()

“弹出窗口”是什么意思?当用户按下按钮时,是否希望打开一个新窗口?或者你是在寻找更像工具文本的东西,当用户将鼠标移到按钮上时会显示出来?我想在另一个窗口上弹出一个新窗口。这个新窗口是你创建的吗?(例如,你有一门课)是的,我上了一门新课。我只是不知道如何绑定它。你能发布一个你的类的缩短版本吗?如何让它显示取决于你如何建立它!我甚至没看到那个。