wxpython 弹出框_绑定到wxButton的wxPython弹出窗口

我一直在四处浏览,浏览了许多不同的站点,但是我找不到如何在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 onm ouseLeftDown(self, evt):

self.Refresh()

self.ldPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())

self.wPos = self.ClientToScreen((0,0))

self.panel.CaptureMouse()

def onm ouseMotion(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 onm ouseLeftUp(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()

标签:popupwindow,wxpython,python

来源: https://codeday.me/bug/20191121/2054428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值