wxpython事件events实例入门

发布时间:2020-03-19编辑:脚本学堂
有关wxpython中事件操作的例子,包括wx.scrollevent、wx.sizeevent、wx.keyevent等事件的例子,需要的朋友参考下。

应用 wx.scrollwinevent 事件的一个例子。
该事件是在点击内建的滚动条时产生的。
而通过调用 setscrollbar() 方法,就可以激活内建的滚动条。
对于独立滚动条,对应的事件类型叫做 wx.scrollevent。

专题教程:wxpython中文教程

例子:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8
# www.plcxue.com
import wx

class MyScrollWinEvent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        panel = wx.Panel(self, -1)
        self.st = wx.StaticText(panel, -1, '0', (30, 0))
        panel.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
        panel.SetScrollbar(wx.VERTICAL, 0, 6, 50)
        self.Center()
       
    def OnScroll(self, evt):
        y = evt.GetPosition()
        self.st.SetLabel(str(y))
       
class MyApp(wx.App):
    def OnInit(self):
        msw = MyScrollWinEvent(None, -1, 'myscrollwinevent.py')
        msw.Show(True)
        self.SetTopWindow(msw)
        return True
   
app = MyApp(0)
app.MainLoop()

当窗口被缩放时,wx.SizeEvent 就产生了。
本例中,将在标题栏显示窗口的大小。

例子:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

#sizeevent.py

import wx

class SizeEvent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
       
        self.Bind(wx.EVT_SIZE, self.OnSize)
       
        self.Center()

    def OnSize(self, event):
        self.SetTitle(str(event.GetSize()))
       
class MyApp(wx.App):
    def OnInit(self):
        se = SizeEvent(None, -1, 'sizeevent.py')
        se.Show(True)
        return True
   
app = MyApp(0)
app.MainLoop()

例3:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

#moveevent.py

import wx

class MoveEvent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
       
        wx.StaticText(self, -1, 'x:', (10, 0))
        wx.StaticText(self, -1, 'y:', (10, 20))
        self.st1 = wx.StaticText(self, -1, '', (30, 0))
        self.st2 = wx.StaticText(self, -1, '', (30, 20))
        self.Bind(wx.EVT_MOVE, self.OnMove)
        self.Center()
       
    def OnMove(self, event):
        x, y = event.GetPosition()
        self.st1.SetLabel(str(x))
        self.st2.SetLabel(str(y))
       
class MyApp(wx.App):
    def OnInit(self):
        me = MoveEvent(None, -1, 'moveevent.py')
        me.Show(True)
        return True
   
app = MyApp(0)
app.MainLoop()
 

在窗口被重新绘制 (redrawn)时,绘制 (paint) 事件就产生了。而重绘发生在缩放或最大化窗口时。
程序本身也会导致产生窗口绘制事件。
比如在调用 SetLabel() 方法来改变某个 wx.StaticText部件时。

注意,最小化窗口时,并不会产生绘制事件。

例子:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

#paintevent.py

import wx

class PaintEvent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
       
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Center()
       
    def OnPaint(self, event):
        wx.Bell()
       
class MyApp(wx.App):
    def OnInit(self):
        pe = PaintEvent(None, -1, 'paintevent.py')
        pe.Show(True)
        return True
   
app = MyApp(0)
app.MainLoop()

在敲下键盘上的一个按键时,wx.KeyEvent 就产生了.
按键处理器共有三个:
EVT_KEY_DOWN
EVT_KEY_UP
EVT_CHAR

通常的操作是按下 Esc 键来关闭程序.
例子:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

import wx

class KeyEvent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        panel = wx.Panel(self, -1)
        panel.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        panel.SetFocus()
       
        self.Center()
        self.Show(True)
       
    def OnKeyDown(self, event):
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_ESCAPE:
            ret = wx.MessageBox('你确定要退出吗?', '问题', wx.YES_NO |        wx.CENTER | wx.NO_DEFAULT, self)
            if ret == wx.YES:
                self.Close()
            event.Skip()
           
app = wx.App()
KeyEvent(None, -1, 'keyevent.py')
app.MainLoop()

通过调用 GetKeyCode() 方法,找出按下的按键.
这里按下的按键代码(keycode) 就是 wx.WXK_ESCAPE.

keycode = event.GetKeyCode()
其它按键代码:
 

WXK_BACK
WXK_DELETE
WXK_MBUTTON
WXK_PAUSE
WXK_HOME
WXK_SELECT
WXK_HELP
WXK_NUMPAD4
WXK_NUMPAD9
WXK_DECIMAL
WXK_F4
WXK_F9
WXK_F14
WXK_F19
WXK_F24
WXK_NUMPAD_SPACE
WXK_NUMPAD_F3
WXK_NUMPAD_RIGHT
WXK_NUMPAD_PAGEDOWN
WXK_NUMPAD_EQUAL
WXK_NUMPAD_SUBTRACT
WXK_TAB
WXK_START
WXK_CLEAR
WXK_CAPITAL
WXK_LEFT
WXK_PRINT
WXK_NUMPAD0
WXK_NUMPAD5
WXK_MULTIPLY
WXK_DIVIDE
WXK_F5
WXK_F10
WXK_F15
WXK_F20
WXK_NUMLOCK
WXK_NUMPAD_TAB
WXK_NUMPAD_F4
WXK_NUMPAD_DOWN
WXK_NUMPAD_END
WXK_NUMPAD_MULTIPLY
WXK_RETURN
WXK_LBUTTON
WXK_SHIFT
WXK_PRIOR
WXK_UP
WXK_EXECUTE
WXK_NUMPAD1
WXK_NUMPAD6
WXK_ADD
WXK_F1
WXK_F6
WXK_F11
WXK_F16
WXK_F21
WXK_SCROLL
WXK_NUMPAD_ENTER
WXK_NUMPAD_HOME
WXK_NUMPAD_PRIOR
WXK_NUMPAD_BEGIN
WXK_NUMPAD_ADD
WXK_NUMPAD_DECIMAL
WXK_ESCAPE
WXK_RBUTTON
WXK_CONTROL
WXK_NEXT
WXK_RIGHT
WXK_SNAPSHOT
WXK_NUMPAD2
WXK_NUMPAD7
WXK_SEPARATOR
WXK_F2
WXK_F7
WXK_F12
WXK_F17
WXK_F22
WXK_PAGEUP
WXK_NUMPAD_F1
WXK_NUMPAD_LEFT
WXK_NUMPAD_PAGEUP
WXK_NUMPAD_INSERT
WXK_NUMPAD_SEPARATOR
WXK_NUMPAD_DIVIDE
WXK_SPACE
WXK_CANCEL
WXK_MENU
WXK_END
WXK_DOWN
WXK_INSERT
WXK_NUMPAD3
WXK_NUMPAD8
WXK_SUBTRACT
WXK_F3
WXK_F8
WXK_F13
WXK_F18
WXK_F23
WXK_PAGEDOWN
WXK_NUMPAD_F2
WXK_NUMPAD_UP
WXK_NUMPAD_NEXT
WXK_NUMPAD_DELETE