Skip to content

Commit 64d98c3

Browse files
committed
wxpython代码更新
1 parent 54194b4 commit 64d98c3

File tree

6 files changed

+182
-7
lines changed

6 files changed

+182
-7
lines changed

gui/ch01/custom_event.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
"""
4+
Topic: 创建自定义事件并绑定,两个按钮都点击后才产生事件
5+
Desc :
6+
"""
7+
import wx
8+
9+
10+
class TwoButtonEvent(wx.PyCommandEvent):
11+
"""先定义一个自定义事件"""
12+
13+
def __init__(self, evtType, id):
14+
wx.PyCommandEvent.__init__(self, evtType, id)
15+
self.clickCount = 0
16+
17+
def getClickCount(self):
18+
return self.clickCount
19+
20+
def setClickCount(self, count):
21+
self.clickCount = count
22+
23+
24+
EVT_TWO_BUTTON_TYPE = wx.NewEventType() # 创建一个事件类型
25+
EVT_TWO_BUTTON = wx.PyEventBinder(EVT_TWO_BUTTON_TYPE, 1) # 创建一个绑定器对象
26+
27+
28+
class TwoButtonPanel(wx.Panel):
29+
def __init__(self, parent, id=-1, leftText='Left', rightText='Right'):
30+
wx.Panel.__init__(self, parent, id)
31+
self.leftButton = wx.Button(self, label=leftText)
32+
self.rightButton = wx.Button(self, label=rightText, pos=(100, 0))
33+
self.leftClick = False
34+
self.rightClick = False
35+
self.clickCount = 0
36+
# 4 下面两行绑定更低级的事件
37+
self.leftButton.Bind(wx.EVT_LEFT_DOWN, self.OnLeftClick)
38+
self.rightButton.Bind(wx.EVT_LEFT_DOWN, self.OnRightClick)
39+
40+
def OnLeftClick(self, event):
41+
self.leftClick = True
42+
self.OnClick()
43+
event.Skip() # 继续处理
44+
45+
def OnRightClick(self, event):
46+
self.rightClick = True
47+
self.OnClick()
48+
event.Skip()
49+
50+
def OnClick(self):
51+
self.clickCount += 1
52+
if self.leftClick and self.rightClick:
53+
self.leftClick = False
54+
self.rightClick = False
55+
# 创建自定义事件
56+
myevent = TwoButtonEvent(EVT_TWO_BUTTON_TYPE, self.GetId())
57+
myevent.setClickCount(self.clickCount) # 添加数据到事件
58+
self.GetEventHandler().ProcessEvent(myevent) # 处理事件
59+
60+
61+
class CustomEventFrame(wx.Frame):
62+
def __init__(self, parent, id):
63+
wx.Frame.__init__(self, parent, id, 'Click Count: 0', size=(300, 100))
64+
panel = TwoButtonPanel(self)
65+
self.Bind(EVT_TWO_BUTTON, self.OnTwoClick, panel) # 绑定自定义事件
66+
67+
def OnTwoClick(self, event): # 定义一个事件处理器
68+
self.SetTitle('Click count: %s' % event.getClickCount())
69+
70+
71+
def main():
72+
app = wx.App()
73+
frame = CustomEventFrame(parent=None, id=-1)
74+
frame.Show()
75+
app.MainLoop()

gui/ch01/dialogs.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
"""
4+
Topic: 对话框
5+
Desc :
6+
"""
7+
__author__ = 'Xiong Neng'
8+
import wx
9+
10+
11+
def main():
12+
app = wx.App()
13+
# --------------------确认对话框--------------------------
14+
# dlg = wx.MessageDialog(None, 'Is this the coolest thing ever!',
15+
# 'MessageDialog', wx.YES_NO | wx.ICON_QUESTION)
16+
# # wx.ID_YES, wx.ID_NO, wx.ID_CANCEL, wx.ID_OK
17+
# result = dlg.ShowModal()
18+
# ---------------------文本输入对话框-----------------------
19+
# dlg = wx.TextEntryDialog(None, "Who is buried in Grant's tomb?",
20+
# 'A Question', 'Cary Grant')
21+
# if dlg.ShowModal() == wx.ID_OK:
22+
# response = dlg.GetValue()
23+
# ---------------------列表选择对话框-----------------------
24+
dlg = wx.SingleChoiceDialog(None,
25+
'What version of Python are you using?',
26+
'Single Choice',
27+
['1.5.2', '2.0', '2.6.3', '2.7', '2.7.8'])
28+
if dlg.ShowModal() == wx.ID_OK:
29+
response = dlg.GetStringSelection()
30+
print(response)
31+
app.MainLoop()
32+

gui/ch01/event_bind.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
"""
4+
Topic: 事件绑定示例
5+
Desc :
6+
"""
7+
import wx
8+
9+
10+
class MouseEventFrame(wx.Frame):
11+
def __init__(self, parent, id):
12+
wx.Frame.__init__(self, parent, id, 'Show Event Bindings', size=(300, 100))
13+
self.panel = wx.Panel(self)
14+
self.button = wx.Button(self.panel, label='Not Over', pos=(100, 15))
15+
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, self.button) # 绑定button点击事件
16+
self.button.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow) # 光标位于其上事件
17+
self.button.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow) # 光标离开事件
18+
19+
def OnButtonClick(self, event):
20+
self.panel.SetBackgroundColour('Green')
21+
self.panel.Refresh()
22+
23+
def OnEnterWindow(self, event):
24+
self.button.SetLabel('Over Me!')
25+
event.Skip()
26+
27+
def OnLeaveWindow(self, event):
28+
self.button.SetLabel('Not Over')
29+
event.Skip()
30+
31+
class DoubleEventFrame(wx.Frame):
32+
"""同时监听两个事件"""
33+
def __init__(self, parent, id):
34+
wx.Frame.__init__(self, parent, id, 'Show Event Bindings', size=(300, 100))
35+
self.panel = wx.Panel(self)
36+
self.button = wx.Button(self.panel, label='Not Over', pos=(100, 15))
37+
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, self.button) # 绑定button点击事件
38+
self.button.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown) # 鼠标左键点击
39+
40+
def OnButtonClick(self, event):
41+
"""鼠标点击后释放才会有这个Click事件"""
42+
self.panel.SetBackgroundColour('Green')
43+
self.panel.Refresh()
44+
45+
def OnMouseDown(self, event):
46+
self.button.SetLabel('Over Me!')
47+
event.Skip() # 鼠标左键事件会优先产生,这时候Skip()会继续去传递这个事件
48+
49+
def main():
50+
app = wx.App()
51+
# frame = MouseEventFrame(parent=None, id=-1)
52+
frame = DoubleEventFrame(parent=None, id=-1)
53+
frame.Show()
54+
app.MainLoop()
55+

gui/ch01/menu_toolbar.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,26 @@ def __init__(self, parent, id):
2626
# 创建两个菜单
2727
menu1 = wx.Menu()
2828
menuBar.Append(menu1, '&File')
29+
menu1.Append(-1, "&Open...", 'Open new file')
30+
menuItem = menu1.Append(-1, "&Exit...", 'Exit System')
31+
# 菜单项绑定事件
32+
self.Bind(wx.EVT_MENU, self.OnCloseMe, menuItem)
2933
menu2 = wx.Menu()
3034
# 创建菜单项MenuItem
3135
menu2.Append(wx.NewId(), '&Copy', 'Copy in status bar')
32-
menu2.Append(wx.NewId(), 'Cut', '')
33-
menu2.Append(wx.NewId(), 'Paste','')
36+
menu2.Append(wx.NewId(), '&Cut', '')
37+
menu2.Append(wx.NewId(), '&Paste','')
3438
menu2.AppendSeparator()
3539
menu2.Append(wx.NewId(), '&Options', 'Display Options')
3640
menuBar.Append(menu2, '&Edit') # 在菜单栏上附上菜单
3741
self.SetMenuBar(menuBar) # 在Frame上面附加菜单
3842

43+
def OnCloseMe(self, event):
44+
self.Close(True)
45+
3946

4047
def main():
41-
app = wx.PySimpleApp()
48+
app = wx.App()
4249
frame = MenuToobarFrame(parent=None, id=-1)
4350
frame.Show()
4451
app.MainLoop()

gui/ch01/show_mouse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def OnMove(self, event):
2222

2323

2424
def main():
25-
app = wx.PySimpleApp()
25+
app = wx.App()
2626
frame = MyFrame()
2727
frame.Show(True)
2828
app.MainLoop()

gui/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
Desc :
66
"""
77
import ch01.show_pic as showpic
8-
import ch01.close_button as closeb
8+
import ch01.close_button as closebutton
99
import ch01.menu_toolbar as menutool
10+
import ch01.dialogs as dialogs
11+
import ch01.event_bind as eventbind
12+
import ch01.custom_event as customevent
1013

1114
if __name__ == '__main__':
1215
pass
1316
# showpic.main()
14-
# closeb.main()
15-
menutool.main()
17+
# closebutton.main()
18+
# menutool.main()
19+
# dialogs.main()
20+
# eventbind.main()
21+
customevent.main()

0 commit comments

Comments
 (0)