📄 frame.py
字号:
elif id == ID_AUTOCOMP_DOUBLE:
event.Check(win.autoCompleteIncludeDouble)
elif id == ID_CALLTIPS_SHOW:
event.Check(win.autoCallTip)
elif id == ID_CALLTIPS_INSERT:
event.Check(win.callTipInsert)
elif id == ID_WRAP:
event.Check(win.GetWrapMode())
elif id == ID_USEAA:
event.Check(win.GetUseAntiAliasing())
elif id == ID_SHOW_LINENUMBERS:
event.Check(win.lineNumbers)
elif id == ID_AUTO_SAVESETTINGS:
event.Check(self.autoSaveSettings)
event.Enable(self.config is not None)
elif id == ID_SAVESETTINGS:
event.Enable(self.config is not None and
hasattr(self, 'DoSaveSettings'))
elif id == ID_DELSETTINGSFILE:
event.Enable(self.config is not None)
elif id == ID_EXECSTARTUPSCRIPT:
event.Check(self.execStartupScript)
event.Enable(self.config is not None)
elif id == ID_SAVEHISTORY:
event.Check(self.autoSaveHistory)
event.Enable(self.dataDir is not None)
elif id == ID_SAVEHISTORYNOW:
event.Enable(self.dataDir is not None and
hasattr(self, 'SaveHistory'))
elif id == ID_CLEARHISTORY:
event.Enable(self.dataDir is not None)
elif id == ID_EDITSTARTUPSCRIPT:
event.Enable(hasattr(self, 'EditStartupScript'))
event.Enable(self.dataDir is not None)
elif id == ID_FIND:
event.Enable(hasattr(win, 'DoFindNext'))
elif id == ID_FINDNEXT:
event.Enable(hasattr(win, 'DoFindNext'))
else:
event.Enable(False)
except AttributeError:
# This menu option is not supported in the current context.
event.Enable(False)
def OnActivate(self, event):
"""
Event Handler for losing the focus of the Frame. Should close
Autocomplete listbox, if shown.
"""
if not event.GetActive():
# If autocomplete active, cancel it. Otherwise, the
# autocomplete list will stay visible on top of the
# z-order after switching to another application
win = wx.Window.FindFocus()
if hasattr(win, 'AutoCompActive') and win.AutoCompActive():
win.AutoCompCancel()
event.Skip()
def LoadSettings(self, config):
"""Called be derived classes to load settings specific to the Frame"""
pos = wx.Point(config.ReadInt('Window/PosX', -1),
config.ReadInt('Window/PosY', -1))
size = wx.Size(config.ReadInt('Window/Width', -1),
config.ReadInt('Window/Height', -1))
self.SetSize(size)
self.Move(pos)
def SaveSettings(self, config):
"""Called by derived classes to save Frame settings to a wx.Config object"""
# TODO: track position/size so we can save it even if the
# frame is maximized or iconized.
if not self.iconized and not self.IsMaximized():
w, h = self.GetSize()
config.WriteInt('Window/Width', w)
config.WriteInt('Window/Height', h)
px, py = self.GetPosition()
config.WriteInt('Window/PosX', px)
config.WriteInt('Window/PosY', py)
class ShellFrameMixin:
"""
A mix-in class for frames that will have a Shell or a Crust window
and that want to add history, startupScript and other common
functionality.
"""
def __init__(self, config, dataDir):
self.config = config
self.dataDir = dataDir
self.startupScript = os.environ.get('PYTHONSTARTUP')
if not self.startupScript and self.dataDir:
self.startupScript = os.path.join(self.dataDir, 'startup')
self.autoSaveSettings = False
self.autoSaveHistory = False
# We need this one before we have a chance to load the settings...
self.execStartupScript = True
if self.config:
self.execStartupScript = self.config.ReadBool('Options/ExecStartupScript', True)
def OnHelp(self, event):
"""Display a Help window."""
import wx.lib.dialogs
title = 'Help on key bindings'
text = wx.py.shell.HELP_TEXT
dlg = wx.lib.dialogs.ScrolledMessageDialog(self, text, title, size = ((700, 540)))
fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
dlg.GetChildren()[0].SetFont(fnt)
dlg.GetChildren()[0].SetInsertionPoint(0)
dlg.ShowModal()
dlg.Destroy()
def LoadSettings(self):
if self.config is not None:
self.autoSaveSettings = self.config.ReadBool('Options/AutoSaveSettings', False)
self.execStartupScript = self.config.ReadBool('Options/ExecStartupScript', True)
self.autoSaveHistory = self.config.ReadBool('Options/AutoSaveHistory', False)
self.LoadHistory()
def SaveSettings(self):
if self.config is not None:
# always save this one
self.config.WriteBool('Options/AutoSaveSettings', self.autoSaveSettings)
if self.autoSaveSettings:
self.config.WriteBool('Options/AutoSaveHistory', self.autoSaveHistory)
self.config.WriteBool('Options/ExecStartupScript', self.execStartupScript)
if self.autoSaveHistory:
self.SaveHistory()
def SaveHistory(self):
if self.dataDir:
try:
name = os.path.join(self.dataDir, 'history')
f = file(name, 'w')
hist = '\x00\n'.join(self.shell.history)
f.write(hist)
f.close()
except:
d = wx.MessageDialog(self, "Error saving history file.",
"Error", wx.ICON_EXCLAMATION)
d.ShowModal()
d.Destroy()
raise
def LoadHistory(self):
if self.dataDir:
name = os.path.join(self.dataDir, 'history')
if os.path.exists(name):
try:
f = file(name, 'U')
hist = f.read()
f.close()
self.shell.history = hist.split('\x00\n')
dispatcher.send(signal="Shell.loadHistory", history=self.shell.history)
except:
d = wx.MessageDialog(self, "Error loading history file.",
"Error", wx.ICON_EXCLAMATION)
d.ShowModal()
d.Destroy()
def bufferHasChanged(self):
# the shell buffers can always be saved
return True
def bufferSave(self):
import time
appname = wx.GetApp().GetAppName()
default = appname + '-' + time.strftime("%Y%m%d-%H%M.py")
fileName = wx.FileSelector("Save File As", "Saving",
default_filename=default,
default_extension="py",
wildcard="*.py",
flags = wx.SAVE | wx.OVERWRITE_PROMPT)
if not fileName:
return
text = self.shell.GetText()
## This isn't working currently...
## d = wx.MessageDialog(self,u'Save source code only?\nAnswering yes will only save lines starting with >>> and ...',u'Question', wx.YES_NO | wx.ICON_QUESTION)
## yes_no = d.ShowModal()
## if yes_no == wx.ID_YES:
## m = re.findall('^[>\.]{3,3} (.*)\r', text, re.MULTILINE | re.LOCALE)
## text = '\n'.join(m)
## d.Destroy()
try:
f = open(fileName, "w")
f.write(text)
f.close()
except:
d = wx.MessageDialog(self, u'Error saving session',u'Error',
wx.OK | wx.ICON_ERROR)
d.ShowModal()
d.Destroy()
def EditStartupScript(self):
if os.path.exists(self.startupScript):
text = file(self.startupScript, 'U').read()
else:
text = ''
dlg = EditStartupScriptDialog(self, self.startupScript, text)
if dlg.ShowModal() == wx.ID_OK:
text = dlg.GetText()
try:
f = file(self.startupScript, 'w')
f.write(text)
f.close()
except:
d = wx.MessageDialog(self, "Error saving startup file.",
"Error", wx.ICON_EXCLAMATION)
d.ShowModal()
d.Destroy()
class EditStartupScriptDialog(wx.Dialog):
def __init__(self, parent, fileName, text):
wx.Dialog.__init__(self, parent, size=(425,350),
title="Edit Startup Script",
style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
pst = wx.StaticText(self, -1, "Path:")
ptx = wx.TextCtrl(self, -1, fileName, style=wx.TE_READONLY)
self.editor = editwindow.EditWindow(self)
self.editor.SetText(text)
wx.CallAfter(self.editor.SetFocus)
ok = wx.Button(self, wx.ID_OK)
cancel = wx.Button(self, wx.ID_CANCEL)
mainSizer = wx.BoxSizer(wx.VERTICAL)
pthSizer = wx.BoxSizer(wx.HORIZONTAL)
pthSizer.Add(pst, flag=wx.ALIGN_CENTER_VERTICAL)
pthSizer.Add((5,5))
pthSizer.Add(ptx, 1)
mainSizer.Add(pthSizer, 0, wx.EXPAND|wx.ALL, 10)
mainSizer.Add(self.editor, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 10)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer.Add((5,5), 1)
btnSizer.Add(ok)
btnSizer.Add((5,5), 1)
btnSizer.Add(cancel)
btnSizer.Add((5,5), 1)
mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.ALL, 10)
self.SetSizer(mainSizer)
self.Layout()
def GetText(self):
return self.editor.GetText()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -