class EditBoxWindow(Frame): def __init__(self, parent=None, **kwargs): if parent is None: # create a new window parent = Tk() self.parent = parent Frame.__init__(self, parent) self.editbox = MultiCallCreator(TextEditor)(self, **kwargs) self.editbox.pack(side=TOP) self.editbox.add_bindings() self.bind("<<open-config-dialog>>", self.config_dialog) bottom = Frame(parent) # lower left subframe which will contain a textfield and a Search button bottom_left_frame = Frame(bottom) self.textfield = Entry(bottom_left_frame) self.textfield.pack(side=LEFT, fill=X, expand=1) buttonSearch = Button(bottom_left_frame, text='Find next', command=self.find) buttonSearch.pack(side=RIGHT) bottom_left_frame.pack(side=LEFT, expand=1) # lower right subframe which will contain OK and Cancel buttons bottom_right_frame = Frame(bottom) buttonOK = Button(bottom_right_frame, text='OK', command=self.pressedOK) buttonCancel = Button(bottom_right_frame, text='Cancel', command=parent.destroy) buttonOK.pack(side=LEFT, fill=X) buttonCancel.pack(side=RIGHT, fill=X) bottom_right_frame.pack(side=RIGHT, expand=1) bottom.pack(side=TOP) # create a toplevel menu menubar = Menu(self.parent) findmenu = Menu(menubar) findmenu.add_command(label="Find", command=self.editbox.find_event, accelerator="Ctrl+F", underline=0) findmenu.add_command(label="Find again", command=self.editbox.find_again_event, accelerator="Ctrl+G", underline=6) findmenu.add_command(label="Find all", command=self.find_all, underline=5) findmenu.add_command(label="Find selection", command=self.editbox.find_selection_event, accelerator="Ctrl+F3", underline=5) findmenu.add_command(label="Replace", command=self.editbox.replace_event, accelerator="Ctrl+H", underline=0) menubar.add_cascade(label="Find", menu=findmenu, underline=0) editmenu = Menu(menubar) editmenu.add_command(label="Cut", command=self.editbox.cut, accelerator="Ctrl+X", underline=2) editmenu.add_command(label="Copy", command=self.editbox.copy, accelerator="Ctrl+C", underline=0) editmenu.add_command(label="Paste", command=self.editbox.paste, accelerator="Ctrl+V", underline=0) editmenu.add_separator() editmenu.add_command(label="Select all", command=self.editbox.select_all, accelerator="Ctrl+A", underline=7) editmenu.add_command(label="Clear selection", command=self.editbox.remove_selection, accelerator="Esc") menubar.add_cascade(label="Edit", menu=editmenu, underline=0) optmenu = Menu(menubar) optmenu.add_command(label="Settings...", command=self.config_dialog, underline=0) menubar.add_cascade(label="Options", menu=optmenu, underline=0) # display the menu self.parent.config(menu=menubar) self.pack() def edit(self, text, jumpIndex=None, highlight=None): """ Parameters: * text - a Unicode string * jumpIndex - an integer: position at which to put the caret * highlight - a substring; each occurence will be highlighted """ self.text = None # put given text into our textarea self.editbox.insert(END, text) # wait for user to push a button which will destroy (close) the window # enable word wrap self.editbox.tag_add('all', '1.0', END) self.editbox.tag_config('all', wrap=WORD) # start search if required if highlight: self.find_all(highlight) if jumpIndex: print jumpIndex line = text[:jumpIndex].count( '\n') + 1 # lines are indexed starting at 1 column = jumpIndex - (text[:jumpIndex].rfind('\n') + 1) # don't know how to place the caret, but scrolling to the right line # should already be helpful. self.editbox.see('%d.%d' % (line, column)) # wait for user to push a button which will destroy (close) the window self.parent.mainloop() return self.text def find_all(self, target): self.textfield.insert(END, target) self.editbox.find_all(target) def find(self): # get text to search for s = self.textfield.get() if s: self.editbox.find_all(s) def config_dialog(self, event=None): configDialog.ConfigDialog(self, 'Settings') def pressedOK(self): # called when user pushes the OK button. # saves the buffer into a variable, and closes the window. self.text = self.editbox.get('1.0', END) # if the editbox contains ASCII characters only, get() will # return string, otherwise unicode (very annoying). We only want # it to return unicode, so we work around this. if isinstance(self.text, str): self.text = unicode(self.text) self.parent.destroy() def debug(self, event=None): self.quit() return "break"
def __init__(self, parent=None, **kwargs): if parent is None: # create a new window parent = Tk() self.parent = parent Frame.__init__(self, parent) self.editbox = MultiCallCreator(TextEditor)(self, **kwargs) self.editbox.pack(side=TOP) self.editbox.add_bindings() self.bind("<<open-config-dialog>>", self.config_dialog) bottom = Frame(parent) # lower left subframe which will contain a textfield and a Search button bottom_left_frame = Frame(bottom) self.textfield = Entry(bottom_left_frame) self.textfield.pack(side=LEFT, fill=X, expand=1) buttonSearch = Button(bottom_left_frame, text='Find next', command=self.find) buttonSearch.pack(side=RIGHT) bottom_left_frame.pack(side=LEFT, expand=1) # lower right subframe which will contain OK and Cancel buttons bottom_right_frame = Frame(bottom) buttonOK = Button(bottom_right_frame, text='OK', command=self.pressedOK) buttonCancel = Button(bottom_right_frame, text='Cancel', command=parent.destroy) buttonOK.pack(side=LEFT, fill=X) buttonCancel.pack(side=RIGHT, fill=X) bottom_right_frame.pack(side=RIGHT, expand=1) bottom.pack(side=TOP) # create a toplevel menu menubar = Menu(self.parent) findmenu = Menu(menubar) findmenu.add_command(label="Find", command=self.editbox.find_event, accelerator="Ctrl+F", underline=0) findmenu.add_command(label="Find again", command=self.editbox.find_again_event, accelerator="Ctrl+G", underline=6) findmenu.add_command(label="Find all", command=self.find_all, underline=5) findmenu.add_command(label="Find selection", command=self.editbox.find_selection_event, accelerator="Ctrl+F3", underline=5) findmenu.add_command(label="Replace", command=self.editbox.replace_event, accelerator="Ctrl+H", underline=0) menubar.add_cascade(label="Find", menu=findmenu, underline=0) editmenu = Menu(menubar) editmenu.add_command(label="Cut", command=self.editbox.cut, accelerator="Ctrl+X", underline=2) editmenu.add_command(label="Copy", command=self.editbox.copy, accelerator="Ctrl+C", underline=0) editmenu.add_command(label="Paste", command=self.editbox.paste, accelerator="Ctrl+V", underline=0) editmenu.add_separator() editmenu.add_command(label="Select all", command=self.editbox.select_all, accelerator="Ctrl+A", underline=7) editmenu.add_command(label="Clear selection", command=self.editbox.remove_selection, accelerator="Esc") menubar.add_cascade(label="Edit", menu=editmenu, underline=0) optmenu = Menu(menubar) optmenu.add_command(label="Settings...", command=self.config_dialog, underline=0) menubar.add_cascade(label="Options", menu=optmenu, underline=0) # display the menu self.parent.config(menu=menubar) self.pack()
def __init__(self, parent=None, **kwargs): """Constructor.""" if parent is None: # create a new window parent = Tkinter.Tk() self.parent = parent Tkinter.Frame.__init__(self, parent) self.editbox = MultiCallCreator(TextEditor)(self, **kwargs) self.editbox.pack(side=Tkinter.TOP) self.editbox.add_bindings() self.bind("<<open-config-dialog>>", self.config_dialog) bottom = Tkinter.Frame(parent) # lower left subframe which will contain a textfield and a Search button bottom_left_frame = Tkinter.Frame(bottom) self.textfield = Tkinter.Entry(bottom_left_frame) self.textfield.pack(side=Tkinter.LEFT, fill=Tkinter.X, expand=1) buttonSearch = Tkinter.Button(bottom_left_frame, text='Find next', command=self.find) buttonSearch.pack(side=Tkinter.RIGHT) bottom_left_frame.pack(side=Tkinter.LEFT, expand=1) # lower right subframe which will contain OK and Cancel buttons bottom_right_frame = Tkinter.Frame(bottom) buttonOK = Tkinter.Button(bottom_right_frame, text='OK', command=self.pressedOK) buttonCancel = Tkinter.Button(bottom_right_frame, text='Cancel', command=parent.destroy) buttonOK.pack(side=Tkinter.LEFT, fill=Tkinter.X) buttonCancel.pack(side=Tkinter.RIGHT, fill=Tkinter.X) bottom_right_frame.pack(side=Tkinter.RIGHT, expand=1) bottom.pack(side=Tkinter.TOP) # create a toplevel menu menubar = Tkinter.Menu(self.parent) findmenu = Tkinter.Menu(menubar) findmenu.add_command(label="Find", command=self.editbox.find_event, accelerator="Ctrl+F", underline=0) findmenu.add_command(label="Find again", command=self.editbox.find_again_event, accelerator="Ctrl+G", underline=6) findmenu.add_command(label="Find all", command=self.find_all, underline=5) findmenu.add_command(label="Find selection", command=self.editbox.find_selection_event, accelerator="Ctrl+F3", underline=5) findmenu.add_command(label="Replace", command=self.editbox.replace_event, accelerator="Ctrl+H", underline=0) menubar.add_cascade(label="Find", menu=findmenu, underline=0) editmenu = Tkinter.Menu(menubar) editmenu.add_command(label="Cut", command=self.editbox.cut, accelerator="Ctrl+X", underline=2) editmenu.add_command(label="Copy", command=self.editbox.copy, accelerator="Ctrl+C", underline=0) editmenu.add_command(label="Paste", command=self.editbox.paste, accelerator="Ctrl+V", underline=0) editmenu.add_separator() editmenu.add_command(label="Select all", command=self.editbox.select_all, accelerator="Ctrl+A", underline=7) editmenu.add_command(label="Clear selection", command=self.editbox.remove_selection, accelerator="Esc") menubar.add_cascade(label="Edit", menu=editmenu, underline=0) optmenu = Tkinter.Menu(menubar) optmenu.add_command(label="Settings...", command=self.config_dialog, underline=0) menubar.add_cascade(label="Options", menu=optmenu, underline=0) # display the menu self.parent.config(menu=menubar) self.pack()
class EditBoxWindow(Tkinter.Frame): """Edit box window.""" def __init__(self, parent=None, **kwargs): """Constructor.""" if parent is None: # create a new window parent = Tkinter.Tk() self.parent = parent Tkinter.Frame.__init__(self, parent) self.editbox = MultiCallCreator(TextEditor)(self, **kwargs) self.editbox.pack(side=Tkinter.TOP) self.editbox.add_bindings() self.bind("<<open-config-dialog>>", self.config_dialog) bottom = Tkinter.Frame(parent) # lower left subframe which will contain a textfield and a Search button bottom_left_frame = Tkinter.Frame(bottom) self.textfield = Tkinter.Entry(bottom_left_frame) self.textfield.pack(side=Tkinter.LEFT, fill=Tkinter.X, expand=1) buttonSearch = Tkinter.Button(bottom_left_frame, text='Find next', command=self.find) buttonSearch.pack(side=Tkinter.RIGHT) bottom_left_frame.pack(side=Tkinter.LEFT, expand=1) # lower right subframe which will contain OK and Cancel buttons bottom_right_frame = Tkinter.Frame(bottom) buttonOK = Tkinter.Button(bottom_right_frame, text='OK', command=self.pressedOK) buttonCancel = Tkinter.Button(bottom_right_frame, text='Cancel', command=parent.destroy) buttonOK.pack(side=Tkinter.LEFT, fill=Tkinter.X) buttonCancel.pack(side=Tkinter.RIGHT, fill=Tkinter.X) bottom_right_frame.pack(side=Tkinter.RIGHT, expand=1) bottom.pack(side=Tkinter.TOP) # create a toplevel menu menubar = Tkinter.Menu(self.parent) findmenu = Tkinter.Menu(menubar) findmenu.add_command(label="Find", command=self.editbox.find_event, accelerator="Ctrl+F", underline=0) findmenu.add_command(label="Find again", command=self.editbox.find_again_event, accelerator="Ctrl+G", underline=6) findmenu.add_command(label="Find all", command=self.find_all, underline=5) findmenu.add_command(label="Find selection", command=self.editbox.find_selection_event, accelerator="Ctrl+F3", underline=5) findmenu.add_command(label="Replace", command=self.editbox.replace_event, accelerator="Ctrl+H", underline=0) menubar.add_cascade(label="Find", menu=findmenu, underline=0) editmenu = Tkinter.Menu(menubar) editmenu.add_command(label="Cut", command=self.editbox.cut, accelerator="Ctrl+X", underline=2) editmenu.add_command(label="Copy", command=self.editbox.copy, accelerator="Ctrl+C", underline=0) editmenu.add_command(label="Paste", command=self.editbox.paste, accelerator="Ctrl+V", underline=0) editmenu.add_separator() editmenu.add_command(label="Select all", command=self.editbox.select_all, accelerator="Ctrl+A", underline=7) editmenu.add_command(label="Clear selection", command=self.editbox.remove_selection, accelerator="Esc") menubar.add_cascade(label="Edit", menu=editmenu, underline=0) optmenu = Tkinter.Menu(menubar) optmenu.add_command(label="Settings...", command=self.config_dialog, underline=0) menubar.add_cascade(label="Options", menu=optmenu, underline=0) # display the menu self.parent.config(menu=menubar) self.pack() def edit(self, text, jumpIndex=None, highlight=None): """ Provide user with editor to modify text. @param text: the text to be edited @type text: unicode @param jumpIndex: position at which to put the caret @type jumpIndex: int @param highlight: each occurrence of this substring will be highlighted @type highlight: unicode @return: the modified text, or None if the user didn't save the text file in his text editor @rtype: unicode or None """ self.text = None # put given text into our textarea self.editbox.insert(Tkinter.END, text) # wait for user to push a button which will destroy (close) the window # enable word wrap self.editbox.tag_add('all', '1.0', Tkinter.END) self.editbox.tag_config('all', wrap=Tkinter.WORD) # start search if required if highlight: self.find_all(highlight) if jumpIndex: # lines are indexed starting at 1 line = text[:jumpIndex].count('\n') + 1 column = jumpIndex - (text[:jumpIndex].rfind('\n') + 1) # don't know how to place the caret, but scrolling to the right line # should already be helpful. self.editbox.see('%d.%d' % (line, column)) # wait for user to push a button which will destroy (close) the window self.parent.mainloop() return self.text def find_all(self, target): """Perform find all operation.""" self.textfield.insert(Tkinter.END, target) self.editbox.find_all(target) def find(self): """Perform find operation.""" # get text to search for s = self.textfield.get() if s: self.editbox.find_all(s) def config_dialog(self, event=None): """Show config dialog.""" configDialog.ConfigDialog(self, 'Settings') def pressedOK(self): """ Perform OK operation. Called when user pushes the OK button. Saves the buffer into a variable, and closes the window. """ self.text = self.editbox.get('1.0', Tkinter.END) # if the editbox contains ASCII characters only, get() will # return string, otherwise unicode (very annoying). We only want # it to return unicode, so we work around this. if PY2 and isinstance(self.text, str): self.text = UnicodeType(self.text) self.parent.destroy() def debug(self, event=None): """Call quit() and return 'break'.""" self.quit() return "break"
def __init__(self, flist=None, filename=None, key=None, root=None): if EditorWindow.help_url is None: dochome = os.path.join(sys.prefix, 'Doc', 'index.html') if sys.platform.count('linux'): pyver = 'python-docs-' + '%s.%s.%s' % sys.version_info[:3] if os.path.isdir('/var/www/html/python/'): dochome = '/var/www/html/python/index.html' else: basepath = '/usr/share/doc/' dochome = os.path.join(basepath, pyver, 'Doc', 'index.html') elif sys.platform[:3] == 'win': chmfile = os.path.join(sys.prefix, 'Doc', 'Python%s.chm' % _sphinx_version()) if os.path.isfile(chmfile): dochome = chmfile elif macosxSupport.runningAsOSXApp(): dochome = os.path.join(sys.prefix, 'Resources/English.lproj/Documentation/index.html') dochome = os.path.normpath(dochome) if os.path.isfile(dochome): EditorWindow.help_url = dochome if sys.platform == 'darwin': EditorWindow.help_url = 'file://' + EditorWindow.help_url else: EditorWindow.help_url = 'http://docs.python.org/%d.%d' % sys.version_info[:2] currentTheme = idleConf.CurrentTheme() self.flist = flist root = root or flist.root self.root = root try: sys.ps1 except AttributeError: sys.ps1 = '>>> ' self.menubar = Menu(root) self.top = top = WindowList.ListedToplevel(root, menu=self.menubar) if flist: self.tkinter_vars = flist.vars self.top.instance_dict = flist.inversedict else: self.tkinter_vars = {} self.top.instance_dict = {} self.recent_files_path = os.path.join(idleConf.GetUserCfgDir(), 'recent-files.lst') self.text_frame = text_frame = Frame(top) self.vbar = vbar = Scrollbar(text_frame, name='vbar') self.width = idleConf.GetOption('main', 'EditorWindow', 'width') text_options = {'name': 'text', 'padx': 5, 'wrap': 'none', 'width': self.width, 'height': idleConf.GetOption('main', 'EditorWindow', 'height') } if TkVersion >= 8.5: text_options['tabstyle'] = 'wordprocessor' self.text = text = MultiCallCreator(Text)(text_frame, **text_options) self.top.focused_widget = self.text self.createmenubar() self.apply_bindings() self.top.protocol('WM_DELETE_WINDOW', self.close) self.top.bind('<<close-window>>', self.close_event) if macosxSupport.runningAsOSXApp(): text.bind('<<close-window>>', self.close_event) text.bind('<Control-Button-1>', self.right_menu_event) else: text.bind('<3>', self.right_menu_event) text.bind('<<cut>>', self.cut) text.bind('<<copy>>', self.copy) text.bind('<<paste>>', self.paste) text.bind('<<center-insert>>', self.center_insert_event) text.bind('<<help>>', self.help_dialog) text.bind('<<python-docs>>', self.python_docs) text.bind('<<about-idle>>', self.about_dialog) text.bind('<<open-config-dialog>>', self.config_dialog) text.bind('<<open-module>>', self.open_module) text.bind('<<do-nothing>>', lambda event: 'break') text.bind('<<select-all>>', self.select_all) text.bind('<<remove-selection>>', self.remove_selection) text.bind('<<find>>', self.find_event) text.bind('<<find-again>>', self.find_again_event) text.bind('<<find-in-files>>', self.find_in_files_event) text.bind('<<find-selection>>', self.find_selection_event) text.bind('<<replace>>', self.replace_event) text.bind('<<goto-line>>', self.goto_line_event) text.bind('<<smart-backspace>>', self.smart_backspace_event) text.bind('<<newline-and-indent>>', self.newline_and_indent_event) text.bind('<<smart-indent>>', self.smart_indent_event) text.bind('<<indent-region>>', self.indent_region_event) text.bind('<<dedent-region>>', self.dedent_region_event) text.bind('<<comment-region>>', self.comment_region_event) text.bind('<<uncomment-region>>', self.uncomment_region_event) text.bind('<<tabify-region>>', self.tabify_region_event) text.bind('<<untabify-region>>', self.untabify_region_event) text.bind('<<toggle-tabs>>', self.toggle_tabs_event) text.bind('<<change-indentwidth>>', self.change_indentwidth_event) text.bind('<Left>', self.move_at_edge_if_selection(0)) text.bind('<Right>', self.move_at_edge_if_selection(1)) text.bind('<<del-word-left>>', self.del_word_left) text.bind('<<del-word-right>>', self.del_word_right) text.bind('<<beginning-of-line>>', self.home_callback) if flist: flist.inversedict[self] = key if key: flist.dict[key] = self text.bind('<<open-new-window>>', self.new_callback) text.bind('<<close-all-windows>>', self.flist.close_all_callback) text.bind('<<open-class-browser>>', self.open_class_browser) text.bind('<<open-path-browser>>', self.open_path_browser) self.set_status_bar() vbar['command'] = text.yview vbar.pack(side=RIGHT, fill=Y) text['yscrollcommand'] = vbar.set fontWeight = 'normal' if idleConf.GetOption('main', 'EditorWindow', 'font-bold', type='bool'): fontWeight = 'bold' text.config(font=(idleConf.GetOption('main', 'EditorWindow', 'font'), idleConf.GetOption('main', 'EditorWindow', 'font-size'), fontWeight)) text_frame.pack(side=LEFT, fill=BOTH, expand=1) text.pack(side=TOP, fill=BOTH, expand=1) text.focus_set() usespaces = idleConf.GetOption('main', 'Indent', 'use-spaces', type='bool') self.usetabs = not usespaces self.tabwidth = 8 self.indentwidth = self.tabwidth self.set_notabs_indentwidth() self.context_use_ps1 = False self.num_context_lines = (50, 500, 5000000) self.per = per = self.Percolator(text) self.undo = undo = self.UndoDelegator() per.insertfilter(undo) text.undo_block_start = undo.undo_block_start text.undo_block_stop = undo.undo_block_stop undo.set_saved_change_hook(self.saved_change_hook) self.io = io = self.IOBinding(self) io.set_filename_change_hook(self.filename_change_hook) self.recent_files_menu = Menu(self.menubar) self.menudict['file'].insert_cascade(3, label='Recent Files', underline=0, menu=self.recent_files_menu) self.update_recent_files_list() self.color = None if filename: if os.path.exists(filename) and not os.path.isdir(filename): io.loadfile(filename) else: io.set_filename(filename) self.ResetColorizer() self.saved_change_hook() self.set_indentation_params(self.ispythonsource(filename)) self.load_extensions() menu = self.menudict.get('windows') if menu: end = menu.index('end') if end is None: end = -1 if end >= 0: menu.add_separator() end = end + 1 self.wmenu_end = end WindowList.register_callback(self.postwindowsmenu) self.askyesno = tkMessageBox.askyesno self.askinteger = tkSimpleDialog.askinteger self.showerror = tkMessageBox.showerror return
class EditBoxWindow(Tkinter.Frame): """Edit box window.""" def __init__(self, parent=None, **kwargs): """Constructor.""" if parent is None: # create a new window parent = Tkinter.Tk() self.parent = parent Tkinter.Frame.__init__(self, parent) self.editbox = MultiCallCreator(TextEditor)(self, **kwargs) self.editbox.pack(side=Tkinter.TOP) self.editbox.add_bindings() self.bind("<<open-config-dialog>>", self.config_dialog) bottom = Tkinter.Frame(parent) # lower left subframe which will contain a textfield and a Search button bottom_left_frame = Tkinter.Frame(bottom) self.textfield = Tkinter.Entry(bottom_left_frame) self.textfield.pack(side=Tkinter.LEFT, fill=Tkinter.X, expand=1) buttonSearch = Tkinter.Button(bottom_left_frame, text='Find next', command=self.find) buttonSearch.pack(side=Tkinter.RIGHT) bottom_left_frame.pack(side=Tkinter.LEFT, expand=1) # lower right subframe which will contain OK and Cancel buttons bottom_right_frame = Tkinter.Frame(bottom) buttonOK = Tkinter.Button(bottom_right_frame, text='OK', command=self.pressedOK) buttonCancel = Tkinter.Button(bottom_right_frame, text='Cancel', command=parent.destroy) buttonOK.pack(side=Tkinter.LEFT, fill=Tkinter.X) buttonCancel.pack(side=Tkinter.RIGHT, fill=Tkinter.X) bottom_right_frame.pack(side=Tkinter.RIGHT, expand=1) bottom.pack(side=Tkinter.TOP) # create a toplevel menu menubar = Tkinter.Menu(self.parent) findmenu = Tkinter.Menu(menubar) findmenu.add_command(label="Find", command=self.editbox.find_event, accelerator="Ctrl+F", underline=0) findmenu.add_command(label="Find again", command=self.editbox.find_again_event, accelerator="Ctrl+G", underline=6) findmenu.add_command(label="Find all", command=self.find_all, underline=5) findmenu.add_command(label="Find selection", command=self.editbox.find_selection_event, accelerator="Ctrl+F3", underline=5) findmenu.add_command(label="Replace", command=self.editbox.replace_event, accelerator="Ctrl+H", underline=0) menubar.add_cascade(label="Find", menu=findmenu, underline=0) editmenu = Tkinter.Menu(menubar) editmenu.add_command(label="Cut", command=self.editbox.cut, accelerator="Ctrl+X", underline=2) editmenu.add_command(label="Copy", command=self.editbox.copy, accelerator="Ctrl+C", underline=0) editmenu.add_command(label="Paste", command=self.editbox.paste, accelerator="Ctrl+V", underline=0) editmenu.add_separator() editmenu.add_command(label="Select all", command=self.editbox.select_all, accelerator="Ctrl+A", underline=7) editmenu.add_command(label="Clear selection", command=self.editbox.remove_selection, accelerator="Esc") menubar.add_cascade(label="Edit", menu=editmenu, underline=0) optmenu = Tkinter.Menu(menubar) optmenu.add_command(label="Settings...", command=self.config_dialog, underline=0) menubar.add_cascade(label="Options", menu=optmenu, underline=0) # display the menu self.parent.config(menu=menubar) self.pack() def edit(self, text, jumpIndex=None, highlight=None): """ Provide user with editor to modify text. @param text: the text to be edited @type text: unicode @param jumpIndex: position at which to put the caret @type jumpIndex: int @param highlight: each occurrence of this substring will be highlighted @type highlight: unicode @return: the modified text, or None if the user didn't save the text file in his text editor @rtype: unicode or None """ self.text = None # put given text into our textarea self.editbox.insert(Tkinter.END, text) # wait for user to push a button which will destroy (close) the window # enable word wrap self.editbox.tag_add('all', '1.0', Tkinter.END) self.editbox.tag_config('all', wrap=Tkinter.WORD) # start search if required if highlight: self.find_all(highlight) if jumpIndex: # lines are indexed starting at 1 line = text[:jumpIndex].count('\n') + 1 column = jumpIndex - (text[:jumpIndex].rfind('\n') + 1) # don't know how to place the caret, but scrolling to the right line # should already be helpful. self.editbox.see('%d.%d' % (line, column)) # wait for user to push a button which will destroy (close) the window self.parent.mainloop() return self.text def find_all(self, target): """Perform find all operation.""" self.textfield.insert(Tkinter.END, target) self.editbox.find_all(target) def find(self): """Perform find operation.""" # get text to search for s = self.textfield.get() if s: self.editbox.find_all(s) def config_dialog(self, event=None): """Show config dialog.""" configDialog.ConfigDialog(self, 'Settings') def pressedOK(self): """ Perform OK operation. Called when user pushes the OK button. Saves the buffer into a variable, and closes the window. """ self.text = self.editbox.get('1.0', Tkinter.END) # if the editbox contains ASCII characters only, get() will # return string, otherwise unicode (very annoying). We only want # it to return unicode, so we work around this. if sys.version[0] == 2 and isinstance(self.text, str): self.text = unicode(self.text) # noqa self.parent.destroy() def debug(self, event=None): """Call quit() and return 'break'.""" self.quit() return "break"
class EditBoxWindow(Frame): def __init__(self, parent=None, **kwargs): if parent is None: # create a new window parent = Tk() self.parent = parent Frame.__init__(self, parent) self.editbox = MultiCallCreator(TextEditor)(self, **kwargs) self.editbox.pack(side=TOP) self.editbox.add_bindings() self.bind("<<open-config-dialog>>", self.config_dialog) bottom = Frame(parent) # lower left subframe which will contain a textfield and a Search button bottom_left_frame = Frame(bottom) self.textfield = Entry(bottom_left_frame) self.textfield.pack(side=LEFT, fill=X, expand=1) buttonSearch = Button(bottom_left_frame, text="Find next", command=self.find) buttonSearch.pack(side=RIGHT) bottom_left_frame.pack(side=LEFT, expand=1) # lower right subframe which will contain OK and Cancel buttons bottom_right_frame = Frame(bottom) buttonOK = Button(bottom_right_frame, text="OK", command=self.pressedOK) buttonCancel = Button(bottom_right_frame, text="Cancel", command=parent.destroy) buttonOK.pack(side=LEFT, fill=X) buttonCancel.pack(side=RIGHT, fill=X) bottom_right_frame.pack(side=RIGHT, expand=1) bottom.pack(side=TOP) # create a toplevel menu menubar = Menu(self.parent) findmenu = Menu(menubar) findmenu.add_command(label="Find", command=self.editbox.find_event, accelerator="Ctrl+F", underline=0) findmenu.add_command( label="Find again", command=self.editbox.find_again_event, accelerator="Ctrl+G", underline=6 ) findmenu.add_command(label="Find all", command=self.find_all, underline=5) findmenu.add_command( label="Find selection", command=self.editbox.find_selection_event, accelerator="Ctrl+F3", underline=5 ) findmenu.add_command(label="Replace", command=self.editbox.replace_event, accelerator="Ctrl+H", underline=0) menubar.add_cascade(label="Find", menu=findmenu, underline=0) editmenu = Menu(menubar) editmenu.add_command(label="Cut", command=self.editbox.cut, accelerator="Ctrl+X", underline=2) editmenu.add_command(label="Copy", command=self.editbox.copy, accelerator="Ctrl+C", underline=0) editmenu.add_command(label="Paste", command=self.editbox.paste, accelerator="Ctrl+V", underline=0) editmenu.add_separator() editmenu.add_command(label="Select all", command=self.editbox.select_all, accelerator="Ctrl+A", underline=7) editmenu.add_command(label="Clear selection", command=self.editbox.remove_selection, accelerator="Esc") menubar.add_cascade(label="Edit", menu=editmenu, underline=0) optmenu = Menu(menubar) optmenu.add_command(label="Settings...", command=self.config_dialog, underline=0) menubar.add_cascade(label="Options", menu=optmenu, underline=0) # display the menu self.parent.config(menu=menubar) self.pack() def edit(self, text, jumpIndex=None, highlight=None): """ Parameters: * text - a Unicode string * jumpIndex - an integer: position at which to put the caret * highlight - a substring; each occurence will be highlighted """ self.text = None # put given text into our textarea self.editbox.insert(END, text) # wait for user to push a button which will destroy (close) the window # enable word wrap self.editbox.tag_add("all", "1.0", END) self.editbox.tag_config("all", wrap=WORD) # start search if required if highlight: self.find_all(highlight) if jumpIndex: print jumpIndex # lines are indexed starting at 1 line = text[:jumpIndex].count("\n") + 1 column = jumpIndex - (text[:jumpIndex].rfind("\n") + 1) # don't know how to place the caret, but scrolling to the right line # should already be helpful. self.editbox.see("%d.%d" % (line, column)) # wait for user to push a button which will destroy (close) the window self.parent.mainloop() return self.text def find_all(self, target): self.textfield.insert(END, target) self.editbox.find_all(target) def find(self): # get text to search for s = self.textfield.get() if s: self.editbox.find_all(s) def config_dialog(self, event=None): configDialog.ConfigDialog(self, "Settings") def pressedOK(self): # called when user pushes the OK button. # saves the buffer into a variable, and closes the window. self.text = self.editbox.get("1.0", END) # if the editbox contains ASCII characters only, get() will # return string, otherwise unicode (very annoying). We only want # it to return unicode, so we work around this. if isinstance(self.text, str): self.text = unicode(self.text) self.parent.destroy() def debug(self, event=None): self.quit() return "break"