Пример #1
0
    def __init__(self, parent=None, **kwargs):
        """Initializer."""
        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 with 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()
Пример #2
0
    def __init__(self, parent=None, **kwargs):
        """Initializer."""
        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 with 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()
Пример #3
0
class EditBoxWindow(Tkinter.Frame):

    """Edit box window."""

    def __init__(self, parent=None, **kwargs):
        """Initializer."""
        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 with 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'
Пример #4
0
class EditBoxWindow(Tkinter.Frame):

    """Edit box window."""

    def __init__(self, parent=None, **kwargs):
        """Initializer."""
        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 with 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"