"""IDLE Configuration Dialog: support user customization of IDLE by GUI
Customize font faces, sizes, and colorization attributes. Set indentation
defaults. Customize keybindings. Colorization and keybindings can be
saved as user defined sets. Select startup options including shell/editor
and default window size. Define additional help sources.
Note that tab width in IDLE is currently fixed at eight due to Tk issues.
Refer to comments in EditorWindow autoindent code for details.
import tkMessageBox, tkColorChooser, tkFont
from idlelib.configHandler import idleConf
from idlelib.dynOptionMenuWidget import DynOptionMenu
from idlelib.keybindingDialog import GetKeysDialog
from idlelib.configSectionNameDialog import GetCfgSectionNameDialog
from idlelib.configHelpSourceEdit import GetHelpSourceDialog
from idlelib.tabbedpages import TabbedPageSet
from idlelib.textView import view_text
from idlelib import macosxSupport
class ConfigDialog(Toplevel):
def __init__(self, parent, title='', _htest=False, _utest=False):
_htest - bool, change box location when running htest
_utest - bool, don't wait_window when running unittest
Toplevel.__init__(self, parent)
parent.instance_dict = {}
self.configure(borderwidth=5)
self.title(title or 'IDLE Preferences')
"+%d+%d" % (parent.winfo_rootx() + 20,
parent.winfo_rooty() + (30 if not _htest else 150)))
#Theme Elements. Each theme element key is its display name.
#The first value of the tuple is the sample area tag name.
#The second value is the display name list sort index.
'Normal Text': ('normal', '00'),
'Python Keywords': ('keyword', '01'),
'Python Definitions': ('definition', '02'),
'Python Builtins': ('builtin', '03'),
'Python Comments': ('comment', '04'),
'Python Strings': ('string', '05'),
'Selected Text': ('hilite', '06'),
'Found Text': ('hit', '07'),
'Cursor': ('cursor', '08'),
'Editor Breakpoint': ('break', '09'),
'Shell Normal Text': ('console', '10'),
'Shell Error Text': ('error', '11'),
'Shell Stdout Text': ('stdout', '12'),
'Shell Stderr Text': ('stderr', '13'),
self.ResetChangedItems() #load initial values in changed items dict
self.resizable(height=FALSE, width=FALSE)
self.protocol("WM_DELETE_WINDOW", self.Cancel)
self.tabPages.focus_set()
#key bindings for this dialog
#self.bind('<Escape>', self.Cancel) #dismiss dialog, no save
#self.bind('<Alt-a>', self.Apply) #apply changes, save
#self.bind('<F1>', self.Help) #context help
self.AttachVarCallbacks() #avoid callbacks during LoadConfigs
self.tabPages = TabbedPageSet(self,
page_names=['Fonts/Tabs', 'Highlighting', 'Keys', 'General',
self.tabPages.pack(side=TOP, expand=TRUE, fill=BOTH)
self.CreatePageHighlight()
self.CreatePageExtensions()
self.create_action_buttons().pack(side=BOTTOM)
def create_action_buttons(self):
if macosxSupport.isAquaTk():
# Changing the default padding on OSX results in unreadable
paddingArgs = {'padx':6, 'pady':3}
outer = Frame(self, pady=2)
buttons = Frame(outer, pady=2)
Button(buttons, text=txt, command=cmd, takefocus=FALSE,
**paddingArgs).pack(side=LEFT, padx=5)
# add space above buttons
Frame(outer, height=2, borderwidth=0).pack(side=TOP)
buttons.pack(side=BOTTOM)
def CreatePageFontTab(self):
self.fontSize = StringVar(parent)
self.fontBold = BooleanVar(parent)
self.fontName = StringVar(parent)
self.spaceNum = IntVar(parent)
self.editFont = tkFont.Font(parent, ('courier', 10, 'normal'))
frame = self.tabPages.pages['Fonts/Tabs'].frame
frame, borderwidth=2, relief=GROOVE, text=' Base Editor Font ')
frameIndent = LabelFrame(
frame, borderwidth=2, relief=GROOVE, text=' Indentation Width ')
frameFontName = Frame(frameFont)
frameFontParam = Frame(frameFont)
labelFontNameTitle = Label(
frameFontName, justify=LEFT, text='Font Face :')
self.listFontName = Listbox(
frameFontName, height=5, takefocus=FALSE, exportselection=FALSE)
'<ButtonRelease-1>', self.OnListFontButtonRelease)
scrollFont = Scrollbar(frameFontName)
scrollFont.config(command=self.listFontName.yview)
self.listFontName.config(yscrollcommand=scrollFont.set)
labelFontSizeTitle = Label(frameFontParam, text='Size :')
self.optMenuFontSize = DynOptionMenu(
frameFontParam, self.fontSize, None, command=self.SetFontSample)
checkFontBold = Checkbutton(
frameFontParam, variable=self.fontBold, onvalue=1,
offvalue=0, text='Bold', command=self.SetFontSample)
frameFontSample = Frame(frameFont, relief=SOLID, borderwidth=1)
self.labelFontSample = Label(
frameFontSample, justify=LEFT, font=self.editFont,
text='AaBbCcDdEe\nFfGgHhIiJjK\n1234567890\n#:+=(){}[]')
frameIndentSize = Frame(frameIndent)
labelSpaceNumTitle = Label(
frameIndentSize, justify=LEFT,
text='Python Standard: 4 Spaces!')
self.scaleSpaceNum = Scale(
frameIndentSize, variable=self.spaceNum,
orient='horizontal', tickinterval=2, from_=2, to=16)
frameFont.pack(side=LEFT, padx=5, pady=5, expand=TRUE, fill=BOTH)
frameIndent.pack(side=LEFT, padx=5, pady=5, fill=Y)
frameFontName.pack(side=TOP, padx=5, pady=5, fill=X)
frameFontParam.pack(side=TOP, padx=5, pady=5, fill=X)
labelFontNameTitle.pack(side=TOP, anchor=W)
self.listFontName.pack(side=LEFT, expand=TRUE, fill=X)
scrollFont.pack(side=LEFT, fill=Y)
labelFontSizeTitle.pack(side=LEFT, anchor=W)
self.optMenuFontSize.pack(side=LEFT, anchor=W)
checkFontBold.pack(side=LEFT, anchor=W, padx=20)
frameFontSample.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH)
self.labelFontSample.pack(expand=TRUE, fill=BOTH)
frameIndentSize.pack(side=TOP, fill=X)
labelSpaceNumTitle.pack(side=TOP, anchor=W, padx=5)
self.scaleSpaceNum.pack(side=TOP, padx=5, fill=X)
def CreatePageHighlight(self):
self.builtinTheme = StringVar(parent)
self.customTheme = StringVar(parent)
self.fgHilite = BooleanVar(parent)
self.colour = StringVar(parent)
self.fontName = StringVar(parent)
self.themeIsBuiltin = BooleanVar(parent)
self.highlightTarget = StringVar(parent)
frame = self.tabPages.pages['Highlighting'].frame
frameCustom = LabelFrame(frame, borderwidth=2, relief=GROOVE,
text=' Custom Highlighting ')
frameTheme = LabelFrame(frame, borderwidth=2, relief=GROOVE,
text=' Highlighting Theme ')
self.textHighlightSample=Text(
frameCustom, relief=SOLID, borderwidth=1,
font=('courier', 12, ''), cursor='hand2', width=21, height=11,
takefocus=FALSE, highlightthickness=0, wrap=NONE)
text=self.textHighlightSample
text.bind('<Double-Button-1>', lambda e: 'break')
text.bind('<B1-Motion>', lambda e: 'break')
('#you can click here', 'comment'), ('\n', 'normal'),
('#to choose items', 'comment'), ('\n', 'normal'),
('def', 'keyword'), (' ', 'normal'),
('func', 'definition'), ('(param):\n ', 'normal'),
('"""string"""', 'string'), ('\n var0 = ', 'normal'),
("'string'", 'string'), ('\n var1 = ', 'normal'),
("'selected'", 'hilite'), ('\n var2 = ', 'normal'),
("'found'", 'hit'), ('\n var3 = ', 'normal'),
('list', 'builtin'), ('(', 'normal'),
('None', 'builtin'), (')\n', 'normal'),
(' breakpoint("line")', 'break'), ('\n\n', 'normal'),
(' error ', 'error'), (' ', 'normal'),
('cursor |', 'cursor'), ('\n ', 'normal'),
('shell', 'console'), (' ', 'normal'),
('stdout', 'stdout'), (' ', 'normal'),
('stderr', 'stderr'), ('\n', 'normal'))
text.insert(END, txTa[0], txTa[1])
for element in self.themeElements:
def tem(event, elem=element):
event.widget.winfo_toplevel().highlightTarget.set(elem)
self.themeElements[element][0], '<ButtonPress-1>', tem)
text.config(state=DISABLED)
self.frameColourSet = Frame(frameCustom, relief=SOLID, borderwidth=1)
frameFgBg = Frame(frameCustom)
buttonSetColour = Button(
self.frameColourSet, text='Choose Colour for :',
command=self.GetColour, highlightthickness=0)
self.optMenuHighlightTarget = DynOptionMenu(
self.frameColourSet, self.highlightTarget, None,
highlightthickness=0) #, command=self.SetHighlightTargetBinding
self.radioFg = Radiobutton(
frameFgBg, variable=self.fgHilite, value=1,
text='Foreground', command=self.SetColourSampleBinding)
self.radioBg=Radiobutton(
frameFgBg, variable=self.fgHilite, value=0,
text='Background', command=self.SetColourSampleBinding)
buttonSaveCustomTheme = Button(
frameCustom, text='Save as New Custom Theme',
command=self.SaveAsNewTheme)
labelTypeTitle = Label(frameTheme, text='Select : ')
self.radioThemeBuiltin = Radiobutton(
frameTheme, variable=self.themeIsBuiltin, value=1,
command=self.SetThemeType, text='a Built-in Theme')
self.radioThemeCustom = Radiobutton(
frameTheme, variable=self.themeIsBuiltin, value=0,
command=self.SetThemeType, text='a Custom Theme')
self.optMenuThemeBuiltin = DynOptionMenu(
frameTheme, self.builtinTheme, None, command=None)
self.optMenuThemeCustom=DynOptionMenu(
frameTheme, self.customTheme, None, command=None)
self.buttonDeleteCustomTheme=Button(
frameTheme, text='Delete Custom Theme',
command=self.DeleteCustomTheme)
self.new_custom_theme = Label(frameTheme, bd=2)
frameCustom.pack(side=LEFT, padx=5, pady=5, expand=TRUE, fill=BOTH)
frameTheme.pack(side=LEFT, padx=5, pady=5, fill=Y)
self.frameColourSet.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=X)
frameFgBg.pack(side=TOP, padx=5, pady=0)
self.textHighlightSample.pack(
side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH)
buttonSetColour.pack(side=TOP, expand=TRUE, fill=X, padx=8, pady=4)
self.optMenuHighlightTarget.pack(
side=TOP, expand=TRUE, fill=X, padx=8, pady=3)
self.radioFg.pack(side=LEFT, anchor=E)
self.radioBg.pack(side=RIGHT, anchor=W)
buttonSaveCustomTheme.pack(side=BOTTOM, fill=X, padx=5, pady=5)
labelTypeTitle.pack(side=TOP, anchor=W, padx=5, pady=5)
self.radioThemeBuiltin.pack(side=TOP, anchor=W, padx=5)
self.radioThemeCustom.pack(side=TOP, anchor=W, padx=5, pady=2)
self.optMenuThemeBuiltin.pack(side=TOP, fill=X, padx=5, pady=5)
self.optMenuThemeCustom.pack(side=TOP, fill=X, anchor=W, padx=5, pady=5)
self.buttonDeleteCustomTheme.pack(side=TOP, fill=X, padx=5, pady=5)
self.new_custom_theme.pack(side=TOP, fill=X, pady=5)
def CreatePageKeys(self):
self.bindingTarget = StringVar(parent)
self.builtinKeys = StringVar(parent)
self.customKeys = StringVar(parent)
self.keysAreBuiltin = BooleanVar(parent)
self.keyBinding = StringVar(parent)
frame = self.tabPages.pages['Keys'].frame
frameCustom = LabelFrame(
frame, borderwidth=2, relief=GROOVE,
text=' Custom Key Bindings ')
frameKeySets = LabelFrame(
frame, borderwidth=2, relief=GROOVE, text=' Key Set ')
frameTarget = Frame(frameCustom)
labelTargetTitle = Label(frameTarget, text='Action - Key(s)')
scrollTargetY = Scrollbar(frameTarget)
scrollTargetX = Scrollbar(frameTarget, orient=HORIZONTAL)
self.listBindings = Listbox(
frameTarget, takefocus=FALSE, exportselection=FALSE)
self.listBindings.bind('<ButtonRelease-1>', self.KeyBindingSelected)
scrollTargetY.config(command=self.listBindings.yview)
scrollTargetX.config(command=self.listBindings.xview)
self.listBindings.config(yscrollcommand=scrollTargetY.set)
self.listBindings.config(xscrollcommand=scrollTargetX.set)
self.buttonNewKeys = Button(
frameCustom, text='Get New Keys for Selection',
command=self.GetNewKeys, state=DISABLED)
frames = [Frame(frameKeySets, padx=2, pady=2, borderwidth=0)
self.radioKeysBuiltin = Radiobutton(
frames[0], variable=self.keysAreBuiltin, value=1,
command=self.SetKeysType, text='Use a Built-in Key Set')
self.radioKeysCustom = Radiobutton(
frames[0], variable=self.keysAreBuiltin, value=0,
command=self.SetKeysType, text='Use a Custom Key Set')
self.optMenuKeysBuiltin = DynOptionMenu(
frames[0], self.builtinKeys, None, command=None)
self.optMenuKeysCustom = DynOptionMenu(
frames[0], self.customKeys, None, command=None)
self.buttonDeleteCustomKeys = Button(
frames[1], text='Delete Custom Key Set',
command=self.DeleteCustomKeys)
buttonSaveCustomKeys = Button(
frames[1], text='Save as New Custom Key Set',
command=self.SaveAsNewKeySet)
frameCustom.pack(side=BOTTOM, padx=5, pady=5, expand=TRUE, fill=BOTH)
frameKeySets.pack(side=BOTTOM, padx=5, pady=5, fill=BOTH)
self.buttonNewKeys.pack(side=BOTTOM, fill=X, padx=5, pady=5)
frameTarget.pack(side=LEFT, padx=5, pady=5, expand=TRUE, fill=BOTH)
frameTarget.columnconfigure(0, weight=1)
frameTarget.rowconfigure(1, weight=1)
labelTargetTitle.grid(row=0, column=0, columnspan=2, sticky=W)
self.listBindings.grid(row=1, column=0, sticky=NSEW)
scrollTargetY.grid(row=1, column=1, sticky=NS)
scrollTargetX.grid(row=2, column=0, sticky=EW)
self.radioKeysBuiltin.grid(row=0, column=0, sticky=W+NS)
self.radioKeysCustom.grid(row=1, column=0, sticky=W+NS)
self.optMenuKeysBuiltin.grid(row=0, column=1, sticky=NSEW)
self.optMenuKeysCustom.grid(row=1, column=1, sticky=NSEW)
self.buttonDeleteCustomKeys.pack(side=LEFT, fill=X, expand=True, padx=2)
buttonSaveCustomKeys.pack(side=LEFT, fill=X, expand=True, padx=2)
frames[0].pack(side=TOP, fill=BOTH, expand=True)
frames[1].pack(side=TOP, fill=X, expand=True, pady=2)
def CreatePageGeneral(self):
self.winWidth = StringVar(parent)
self.winHeight = StringVar(parent)
self.startupEdit = IntVar(parent)
self.autoSave = IntVar(parent)
self.encoding = StringVar(parent)
self.userHelpBrowser = BooleanVar(parent)
self.helpBrowser = StringVar(parent)
frame = self.tabPages.pages['General'].frame
frameRun = LabelFrame(frame, borderwidth=2, relief=GROOVE,
text=' Startup Preferences ')
frameSave = LabelFrame(frame, borderwidth=2, relief=GROOVE,
text=' Autosave Preferences ')
frameWinSize = Frame(frame, borderwidth=2, relief=GROOVE)
frameEncoding = Frame(frame, borderwidth=2, relief=GROOVE)
frameHelp = LabelFrame(frame, borderwidth=2, relief=GROOVE,
text=' Additional Help Sources ')
labelRunChoiceTitle = Label(frameRun, text='At Startup')
radioStartupEdit = Radiobutton(
frameRun, variable=self.startupEdit, value=1,
command=self.SetKeysType, text="Open Edit Window")
radioStartupShell = Radiobutton(
frameRun, variable=self.startupEdit, value=0,
command=self.SetKeysType, text='Open Shell Window')
labelRunSaveTitle = Label(frameSave, text='At Start of Run (F5) ')
radioSaveAsk = Radiobutton(
frameSave, variable=self.autoSave, value=0,
command=self.SetKeysType, text="Prompt to Save")
radioSaveAuto = Radiobutton(
frameSave, variable=self.autoSave, value=1,
command=self.SetKeysType, text='No Prompt')
labelWinSizeTitle = Label(
frameWinSize, text='Initial Window Size (in characters)')
labelWinWidthTitle = Label(frameWinSize, text='Width')
frameWinSize, textvariable=self.winWidth, width=3)
labelWinHeightTitle = Label(frameWinSize, text='Height')
frameWinSize, textvariable=self.winHeight, width=3)
labelEncodingTitle = Label(
frameEncoding, text="Default Source Encoding")
radioEncLocale = Radiobutton(
frameEncoding, variable=self.encoding,
value="locale", text="Locale-defined")
radioEncUTF8 = Radiobutton(
frameEncoding, variable=self.encoding,
value="utf-8", text="UTF-8")
radioEncNone = Radiobutton(
frameEncoding, variable=self.encoding,
value="none", text="None")
frameHelpList = Frame(frameHelp)
frameHelpListButtons = Frame(frameHelpList)
scrollHelpList = Scrollbar(frameHelpList)
frameHelpList, height=5, takefocus=FALSE,
scrollHelpList.config(command=self.listHelp.yview)
self.listHelp.config(yscrollcommand=scrollHelpList.set)
self.listHelp.bind('<ButtonRelease-1>', self.HelpSourceSelected)
self.buttonHelpListEdit = Button(
frameHelpListButtons, text='Edit', state=DISABLED,
width=8, command=self.HelpListItemEdit)
self.buttonHelpListAdd = Button(
frameHelpListButtons, text='Add',
width=8, command=self.HelpListItemAdd)
self.buttonHelpListRemove = Button(
frameHelpListButtons, text='Remove', state=DISABLED,
width=8, command=self.HelpListItemRemove)
frameRun.pack(side=TOP, padx=5, pady=5, fill=X)
frameSave.pack(side=TOP, padx=5, pady=5, fill=X)
frameWinSize.pack(side=TOP, padx=5, pady=5, fill=X)
frameEncoding.pack(side=TOP, padx=5, pady=5, fill=X)
frameHelp.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH)
labelRunChoiceTitle.pack(side=LEFT, anchor=W, padx=5, pady=5)
radioStartupShell.pack(side=RIGHT, anchor=W, padx=5, pady=5)
radioStartupEdit.pack(side=RIGHT, anchor=W, padx=5, pady=5)
labelRunSaveTitle.pack(side=LEFT, anchor=W, padx=5, pady=5)
radioSaveAuto.pack(side=RIGHT, anchor=W, padx=5, pady=5)
radioSaveAsk.pack(side=RIGHT, anchor=W, padx=5, pady=5)
labelWinSizeTitle.pack(side=LEFT, anchor=W, padx=5, pady=5)
entryWinHeight.pack(side=RIGHT, anchor=E, padx=10, pady=5)
labelWinHeightTitle.pack(side=RIGHT, anchor=E, pady=5)
entryWinWidth.pack(side=RIGHT, anchor=E, padx=10, pady=5)
labelWinWidthTitle.pack(side=RIGHT, anchor=E, pady=5)
labelEncodingTitle.pack(side=LEFT, anchor=W, padx=5, pady=5)
radioEncNone.pack(side=RIGHT, anchor=E, pady=5)
radioEncUTF8.pack(side=RIGHT, anchor=E, pady=5)
radioEncLocale.pack(side=RIGHT, anchor=E, pady=5)
frameHelpListButtons.pack(side=RIGHT, padx=5, pady=5, fill=Y)
frameHelpList.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH)
scrollHelpList.pack(side=RIGHT, anchor=W, fill=Y)
self.listHelp.pack(side=LEFT, anchor=E, expand=TRUE, fill=BOTH)
self.buttonHelpListEdit.pack(side=TOP, anchor=W, pady=5)
self.buttonHelpListAdd.pack(side=TOP, anchor=W)
self.buttonHelpListRemove.pack(side=TOP, anchor=W, pady=5)
def AttachVarCallbacks(self):
self.fontSize.trace_variable('w', self.VarChanged_font)
self.fontName.trace_variable('w', self.VarChanged_font)
self.fontBold.trace_variable('w', self.VarChanged_font)
self.spaceNum.trace_variable('w', self.VarChanged_spaceNum)
self.colour.trace_variable('w', self.VarChanged_colour)
self.builtinTheme.trace_variable('w', self.VarChanged_builtinTheme)
self.customTheme.trace_variable('w', self.VarChanged_customTheme)
self.themeIsBuiltin.trace_variable('w', self.VarChanged_themeIsBuiltin)
self.highlightTarget.trace_variable('w', self.VarChanged_highlightTarget)
self.keyBinding.trace_variable('w', self.VarChanged_keyBinding)
self.builtinKeys.trace_variable('w', self.VarChanged_builtinKeys)
self.customKeys.trace_variable('w', self.VarChanged_customKeys)
self.keysAreBuiltin.trace_variable('w', self.VarChanged_keysAreBuiltin)
self.winWidth.trace_variable('w', self.VarChanged_winWidth)
self.winHeight.trace_variable('w', self.VarChanged_winHeight)
self.startupEdit.trace_variable('w', self.VarChanged_startupEdit)
self.autoSave.trace_variable('w', self.VarChanged_autoSave)