Python论坛  - 讨论区

标题:[python-chinese] 求助:wxPython中怎么模拟post wx.Button事件?

2005年06月23日 星期四 07:30

Qiangning Hong hongqn at gmail.com
Thu Jun 23 07:30:25 HKT 2005

amingsc wrote:
> 在wxPython的demo中有post自定义事件的例子,但是对于系统预定义事件我却不知道如何实现,比如:
> 我现在要模拟button的click事件,下面的代码需要怎么改进?
> evt= wx.CommandEvent(commandType=?,winid=ID_BTN)
> wx.PostEvent(win,evt)

commandType=wx.wxEVT_COMMAND_BUTTON_CLICKED

看下面的演示代码,点击btn2会产生一个点击btn1的event:

import wx

class Frame(wx.Frame):
    def __init__(self, *a, **kw):
        super(Frame, self).__init__(*a, **kw)
        self.btn = b = wx.Button(self, -1, 'Click me!')
        b2 = wx.Button(self, -1, 'Click the above button by code!')
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.AddMany([b, b2])
        self.SetSizerAndFit(sizer)
        self.Bind(wx.EVT_BUTTON, self.on_btn, b)
        self.Bind(wx.EVT_BUTTON, self.on_btn2, b2)

    def on_btn(self, evt):
        wx.MessageBox('Clicked')

    def on_btn2(self, evt):
        new_event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED,
               self.btn.GetId())
        wx.PostEvent(self, new_event)


if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame(None, -1, 'Test')
    frame.Show()
    app.MainLoop()


-- 
Qiangning Hong

 __________________________________________________________
/ Q: Know what the difference between your latest project  \
|                                                          |
| and putting wings on an elephant is? urA: Who knows? The |
\ elephant *might* fly, heh, heh...                        /
 ----------------------------------------------------------
   \
    \
    ____
   /# /_\_
  |  |/o\o\
  |  \\_/_/
 / |_   |
|  ||\_ ~|
|  ||| \/
|  |||_
 \//  |
  ||  |
  ||_  \
  \_|  o|
  /\___/
 /  ||||__
    (___)_)

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号