gpt4 book ai didi

python - 绑定(bind)到 wxButton 的 wxPython 弹出窗口

转载 作者:太空狗 更新时间:2023-10-30 03:02:03 27 4
gpt4 key购买 nike

我一直在四处寻找,浏览了一大堆不同的网站,但我无法找到如何在 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()

关于python - 绑定(bind)到 wxButton 的 wxPython 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23415125/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com