我正在尝试遵循书中的以下代码,但我无法理解它。我希望有人可以帮助澄清以下问题:
- 当我在 textctrl 中什么都不做时,has_sel 设置为 false 但是 IF stmt 如何执行?
- 什么是 event_id?它是整个菜单栏的 event_id 吗?
- 为什么 txt_ctrl 中的项目更改时 event_id 会更改?
- 当 event.Enable(has_sel) 为真时,这两个按钮是如何启用/禁用的?
这是代码:
import wx
ID_CHECK_ITEM = wx.NewId()
class TextFrame(wx.Frame):
def __init__(self, parent, *args, **kwargs):
super(TextFrame, self).__init__(parent,
*args,
**kwargs)
# Attributes
self.panel = wx.Panel(self)
self.txtctrl = wx.TextCtrl(self.panel,
value="Hello World",
style=wx.TE_MULTILINE)
# Layout
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.txtctrl, 1, wx.EXPAND)
self.panel.SetSizer(sizer)
self.CreateStatusBar() # For output display
# Menu
menub = wx.MenuBar()
editm = wx.Menu()
editm.Append(wx.ID_COPY, "Copy\tCtrl+C")
editm.Append(wx.ID_CUT, "Cut\tCtrl+X")
editm.Append(ID_CHECK_ITEM, "Selection Made?",
kind=wx.ITEM_CHECK)
menub.Append(editm, "Edit")
self.SetMenuBar(menub)
# Event Handlers
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateEditMenu)
def OnUpdateEditMenu(self, event):
event_id = event.GetId()
sel = self.txtctrl.GetSelection()
print("Start")
print('Sel',sel)
has_sel = sel[0] != sel[1]
print('has_sel=',has_sel)
print('sel[0]=',sel[0])
print('sel[1]=',sel[1])
print('event_id=',event_id)
print('wx.ID_COPY',wx.ID_COPY)
print('wx.ID_CUT=',wx.ID_CUT)
print('ID_CHECK_ITEM=',ID_CHECK_ITEM)
if event_id in (wx.ID_COPY, wx.ID_CUT):
print("Select option set to true-1")
event.Enable(has_sel)
elif event_id == ID_CHECK_ITEM:
print("Select option set to true-2")
event.Check(has_sel)
else:
event.Skip()