wx_test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. import wx
  3. import win32api
  4. import sys, os
  5. APP_TITLE = u'控件事件、鼠标事件、键盘事件、系统事件'
  6. APP_ICON = 'res/python.ico'
  7. class mainFrame(wx.Frame):
  8. '''程序主窗口类,继承自wx.Frame'''
  9. def __init__(self, parent):
  10. '''构造函数'''
  11. wx.Frame.__init__(self, parent, -1, APP_TITLE)
  12. self.SetBackgroundColour(wx.Colour(224, 224, 224))
  13. self.SetSize((520, 220))
  14. self.Center()
  15. if hasattr(sys, "frozen") and getattr(sys, "frozen") == "windows_exe":
  16. exeName = win32api.GetModuleFileName(win32api.GetModuleHandle(None))
  17. icon = wx.Icon(exeName, wx.BITMAP_TYPE_ICO)
  18. else:
  19. icon = wx.Icon(APP_ICON, wx.BITMAP_TYPE_ICO)
  20. self.SetIcon(icon)
  21. wx.StaticText(self, -1, u'第一行输入框:', pos=(40, 50), size=(100, -1), style=wx.ALIGN_RIGHT)
  22. wx.StaticText(self, -1, u'第二行输入框:', pos=(40, 80), size=(100, -1), style=wx.ALIGN_RIGHT)
  23. self.tip = wx.StaticText(self, -1, u'', pos=(145, 110), size=(150, -1), style=wx.ST_NO_AUTORESIZE)
  24. self.tc1 = wx.TextCtrl(self, -1, '', pos=(145, 50), size=(150, -1), name='TC01', style=wx.TE_CENTER)
  25. self.tc2 = wx.TextCtrl(self, -1, '', pos=(145, 80), size=(150, -1), name='TC02',
  26. style=wx.TE_PASSWORD | wx.ALIGN_RIGHT)
  27. btn_mea = wx.Button(self, -1, u'鼠标左键事件', pos=(350, 50), size=(100, 25))
  28. btn_meb = wx.Button(self, -1, u'鼠标所有事件', pos=(350, 80), size=(100, 25))
  29. btn_close = wx.Button(self, -1, u'关闭窗口', pos=(350, 110), size=(100, 25))
  30. # 控件事件
  31. self.tc1.Bind(wx.EVT_TEXT, self.EvtText)
  32. self.tc2.Bind(wx.EVT_TEXT, self.EvtText)
  33. self.Bind(wx.EVT_BUTTON, self.OnClose, btn_close)
  34. # 鼠标事件
  35. btn_mea.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
  36. btn_mea.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
  37. btn_mea.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheel)
  38. btn_meb.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouse)
  39. # 键盘事件
  40. self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
  41. # 系统事件
  42. self.Bind(wx.EVT_CLOSE, self.OnClose)
  43. self.Bind(wx.EVT_SIZE, self.On_size)
  44. # self.Bind(wx.EVT_PAINT, self.On_paint)
  45. # self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None)
  46. def EvtText(self, evt):
  47. '''输入框事件函数'''
  48. obj = evt.GetEventObject()
  49. objName = obj.GetName()
  50. text = evt.GetString()
  51. if objName == 'TC01':
  52. self.tc2.SetValue(text)
  53. elif objName == 'TC02':
  54. self.tc1.SetValue(text)
  55. def On_size(self, evt):
  56. '''改变窗口大小事件函数'''
  57. self.Refresh()
  58. evt.Skip() # 体会作用
  59. def OnClose(self, evt):
  60. '''关闭窗口事件函数'''
  61. dlg = wx.MessageDialog(None, u'确定要关闭本窗口?', u'操作提示', wx.YES_NO | wx.ICON_QUESTION)
  62. if (dlg.ShowModal() == wx.ID_YES):
  63. self.Destroy()
  64. def OnLeftDown(self, evt):
  65. '''左键按下事件函数'''
  66. self.tip.SetLabel(u'左键按下')
  67. def OnLeftUp(self, evt):
  68. '''左键弹起事件函数'''
  69. self.tip.SetLabel(u'左键弹起')
  70. def OnMouseWheel(self, evt):
  71. '''鼠标滚轮事件函数'''
  72. vector = evt.GetWheelRotation()
  73. self.tip.SetLabel(str(vector))
  74. def OnMouse(self, evt):
  75. '''鼠标事件函数'''
  76. self.tip.SetLabel(str(evt.EventType))
  77. def OnKeyDown(self, evt):
  78. '''键盘事件函数'''
  79. key = evt.GetKeyCode()
  80. self.tip.SetLabel(str(key))
  81. class mainApp(wx.App):
  82. def OnInit(self):
  83. self.SetAppName(APP_TITLE)
  84. self.Frame = mainFrame(None)
  85. self.Frame.Show()
  86. return True
  87. if __name__ == "__main__":
  88. app = mainApp()
  89. app.MainLoop()