Пример #1
0
def _color_delegator(parent):  # htest #
    from tkinter import Toplevel, Text
    from idlelib.percolator import Percolator

    top = Toplevel(parent)
    top.title("Test ColorDelegator")
    x, y = map(int, parent.geometry().split('+')[1:])
    top.geometry("700x250+%d+%d" % (x + 20, y + 175))
    source = ("# Following has syntax errors\n"
        "if True: then int 1\nelif False: print 0\nelse: float(None)\n"
        "if iF + If + IF: 'keywork matching must respect case'\n"
        "# All valid prefixes for unicode and byte strings should be colored\n"
        "'x', '''x''', \"x\", \"\"\"x\"\"\"\n"
        "r'x', u'x', R'x', U'x', f'x', F'x', ur'is invalid'\n"
        "fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x'\n"
        "b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x'.rB'x',Rb'x',RB'x'\n")
    text = Text(top, background="white")
    text.pack(expand=1, fill="both")
    text.insert("insert", source)
    text.focus_set()

    color_config(text)
    p = Percolator(text)
    d = ColorDelegator()
    p.insertfilter(d)
Пример #2
0
def _color_delegator(parent):
    from tkinter import Toplevel, Text
    from idlelib.percolator import Percolator
    top = Toplevel(parent)
    top.title('Test ColorDelegator')
    x, y = map(int, parent.geometry().split('+')[1:])
    top.geometry('700x250+%d+%d' % (x + 20, y + 175))
    source = """# Following has syntax errors
if True: then int 1
elif False: print 0
else: float(None)
if iF + If + IF: 'keywork matching must respect case'
# All valid prefixes for unicode and byte strings should be colored
'x', '''x''', "x", ""\"x""\"
r'x', u'x', R'x', U'x', f'x', F'x', ur'is invalid'
fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x'
b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x'.rB'x',Rb'x',RB'x'
"""
    text = Text(top, background='white')
    text.pack(expand=1, fill='both')
    text.insert('insert', source)
    text.focus_set()
    color_config(text)
    p = Percolator(text)
    d = ColorDelegator()
    p.insertfilter(d)
Пример #3
0
def _color_delegator(parent):  # htest #
    from tkinter import Toplevel, Text
    from idlelib.percolator import Percolator

    top = Toplevel(parent)
    top.title("Test ColorDelegator")
    x, y = map(int, parent.geometry().split('+')[1:])
    top.geometry("700x250+%d+%d" % (x + 20, y + 175))
    source = ("# Following has syntax errors\n"
        "if True: then int 1\nelif False: print 0\nelse: float(None)\n"
        "if iF + If + IF: 'keywork matching must respect case'\n"
        "# All valid prefixes for unicode and byte strings should be colored\n"
        "'x', '''x''', \"x\", \"\"\"x\"\"\"\n"
        "r'x', u'x', R'x', U'x', f'x', F'x', ur'is invalid'\n"
        "fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x'\n"
        "b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x'.rB'x',Rb'x',RB'x'\n")
    text = Text(top, background="white")
    text.pack(expand=1, fill="both")
    text.insert("insert", source)
    text.focus_set()

    color_config(text)
    p = Percolator(text)
    d = ColorDelegator()
    p.insertfilter(d)
Пример #4
0
def _color_delegator(parent):  # htest #
    from tkinter import Toplevel, Text
    from idlelib.percolator import Percolator

    top = Toplevel(parent)
    top.title("Test ColorDelegator")
    x, y = map(int, parent.geometry().split('+')[1:])
    top.geometry("700x250+%d+%d" % (x + 20, y + 175))
    source = (
        "if True: int ('1') # keyword, builtin, string, comment\n"
        "elif False: print(0)\n"
        "else: float(None)\n"
        "if iF + If + IF: 'keyword matching must respect case'\n"
        "if'': x or''  # valid string-keyword no-space combinations\n"
        "async def f(): await g()\n"
        "# All valid prefixes for unicode and byte strings should be colored.\n"
        "'x', '''x''', \"x\", \"\"\"x\"\"\"\n"
        "r'x', u'x', R'x', U'x', f'x', F'x'\n"
        "fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x'\n"
        "b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x', rB'x',Rb'x',RB'x'\n"
        "# Invalid combinations of legal characters should be half colored.\n"
        "ur'x', ru'x', uf'x', fu'x', UR'x', ufr'x', rfu'x', xf'x', fx'x'\n"
        )
    text = Text(top, background="white")
    text.pack(expand=1, fill="both")
    text.insert("insert", source)
    text.focus_set()

    color_config(text)
    p = Percolator(text)
    d = ColorDelegator()
    p.insertfilter(d)
Пример #5
0
def _color_delegator(parent):  # htest #
    from tkinter import Toplevel, Text
    from idlelib.percolator import Percolator

    top = Toplevel(parent)
    top.title("Test ColorDelegator")
    x, y = map(int, parent.geometry().split('+')[1:])
    top.geometry("700x250+%d+%d" % (x + 20, y + 175))
    source = (
        "if True: int ('1') # keyword, builtin, string, comment\n"
        "elif False: print(0)\n"
        "else: float(None)\n"
        "if iF + If + IF: 'keyword matching must respect case'\n"
        "if'': x or''  # valid string-keyword no-space combinations\n"
        "# All valid prefixes for unicode and byte strings should be colored.\n"
        "'x', '''x''', \"x\", \"\"\"x\"\"\"\n"
        "r'x', u'x', R'x', U'x', f'x', F'x'\n"
        "fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x'\n"
        "b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x'.rB'x',Rb'x',RB'x'\n"
        "# Invalid combinations of legal characters should be half colored.\n"
        "ur'x', ru'x', uf'x', fu'x', UR'x', ufr'x', rfu'x', xf'x', fx'x'"
        )
    text = Text(top, background="white")
    text.pack(expand=1, fill="both")
    text.insert("insert", source)
    text.focus_set()

    color_config(text)
    p = Percolator(text)
    d = ColorDelegator()
    p.insertfilter(d)
Пример #6
0
 def colour_py(self, inherit_config=True):
     if 'idlelib' in sys.modules:
         col = idlelib.colorizer
         if inherit_config:
             col.color_config(self)
         p = Percolator(self)
         d = col.ColorDelegator()
         p.insertfilter(d)
Пример #7
0
class Dummy_editwin:
    def __init__(self, text):
        self.text = text
        self.text_frame = self.text.master
        self.per = Percolator(text)
        self.undo = Delegator()
        self.per.insertfilter(self.undo)

    def setvar(self, name, value):
        pass

    def getlineno(self, index):
        return int(float(self.text.index(index)))
Пример #8
0
    def __init__(self, master, initial='', width=90, **kwargs):
        super().__init__(master, **kwargs)

        self.text = tk.Text(self,
                            name='text',
                            padx=5,
                            wrap='none',
                            width=width,
                            undo=True)
        vbar = tk.Scrollbar(self, name='vbar')
        vbar['command'] = self.text.yview
        vbar.pack(side=tk.LEFT, fill=tk.Y)
        self.text['yscrollcommand'] = vbar.set

        self.status_bar = MultiStatusBar(self)
        self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)

        self.set_content(initial)
        self.text.edit_reset()
        self.text.pack(side=tk.LEFT, fill=tk.Y, expand=True)

        Percolator(self.text).insertfilter(ColorDelegator())

        self.text.bind("<<set-line-and-column>>", self.set_line_and_column)
        self.text.event_add("<<set-line-and-column>>", "<KeyRelease>",
                            "<ButtonRelease>")
        self.text.after_idle(self.set_line_and_column)

        self.text.event_add("<<set-line-and-column>>", "<KeyRelease>",
                            "<ButtonRelease>")

        self.listeners = []
Пример #9
0
def _color_delegator(parent):  # htest #
    from tkinter import Toplevel, Text
    from idlelib.percolator import Percolator

    top = Toplevel(parent)
    top.title("Test ColorDelegator")
    top.geometry("200x100+%d+%d" % (parent.winfo_rootx() + 200,
                  parent.winfo_rooty() + 150))
    source = "if somename: x = 'abc' # comment\nprint\n"
    text = Text(top, background="white")
    text.pack(expand=1, fill="both")
    text.insert("insert", source)
    text.focus_set()

    color_config(text)
    p = Percolator(text)
    d = ColorDelegator()
    p.insertfilter(d)
Пример #10
0
def _color_delegator(parent):  # htest #
    from tkinter import Toplevel, Text
    from idlelib.percolator import Percolator

    top = Toplevel(parent)
    top.title("Test ColorDelegator")
    x, y = map(int, parent.geometry().split('+')[1:])
    top.geometry("200x100+%d+%d" % (x + 250, y + 175))
    source = "if somename: x = 'abc' # comment\nprint\n"
    text = Text(top, background="white")
    text.pack(expand=1, fill="both")
    text.insert("insert", source)
    text.focus_set()

    color_config(text)
    p = Percolator(text)
    d = ColorDelegator()
    p.insertfilter(d)
Пример #11
0
def create_pack_code_style(codetxt):
    try:
        from idlelib.colorizer import ColorDelegator
        from idlelib.percolator import Percolator
        d = ColorDelegator()
        Percolator(codetxt).insertfilter(d)
    except:
        import traceback
        traceback.print_exc()
Пример #12
0
def _color_delegator(parent):  # htest #
    from tkinter import Toplevel, Text
    from idlelib.idle_test.test_colorizer import source
    from idlelib.percolator import Percolator

    top = Toplevel(parent)
    top.title("Test ColorDelegator")
    x, y = map(int, parent.geometry().split('+')[1:])
    top.geometry("700x550+%d+%d" % (x + 20, y + 175))

    text = Text(top, background="white")
    text.pack(expand=1, fill="both")
    text.insert("insert", source)
    text.focus_set()

    color_config(text)
    p = Percolator(text)
    d = ColorDelegator()
    p.insertfilter(d)
Пример #13
0
def _undo_delegator(parent):
    from tkinter import Toplevel, Text, Button
    from idlelib.percolator import Percolator
    undowin = Toplevel(parent)
    undowin.title('Test UndoDelegator')
    x, y = map(int, parent.geometry().split('+')[1:])
    undowin.geometry('+%d+%d' % (x, y + 175))
    text = Text(undowin, height=10)
    text.pack()
    text.focus_set()
    p = Percolator(text)
    d = UndoDelegator()
    p.insertfilter(d)
    undo = Button(undowin, text='Undo', command=lambda: d.undo_event(None))
    undo.pack(side='left')
    redo = Button(undowin, text='Redo', command=lambda: d.redo_event(None))
    redo.pack(side='left')
    dump = Button(undowin, text='Dump', command=lambda: d.dump_event(None))
    dump.pack(side='left')
Пример #14
0
    def make_mock_squeezer(self):
        """Helper for tests: Create a mock Squeezer object."""
        root = get_test_tk_root(self)
        squeezer = Mock()
        squeezer.editwin.text = Text(root)
        squeezer.editwin.per = Percolator(squeezer.editwin.text)

        # Set default values for the configuration settings.
        squeezer.auto_squeeze_min_lines = 50
        return squeezer
Пример #15
0
def _undo_delegator(parent):  # htest #
    from tkinter import Toplevel, Text, Button
    from idlelib.percolator import Percolator
    undowin = Toplevel(parent)
    undowin.title("Test UndoDelegator")
    x, y = map(int, parent.geometry().split('+')[1:])
    undowin.geometry("+%d+%d" % (x, y + 175))

    text = Text(undowin, height=10)
    text.pack()
    text.focus_set()
    p = Percolator(text)
    d = UndoDelegator()
    p.insertfilter(d)

    undo = Button(undowin, text="Undo", command=lambda:d.undo_event(None))
    undo.pack(side='left')
    redo = Button(undowin, text="Redo", command=lambda:d.redo_event(None))
    redo.pack(side='left')
    dump = Button(undowin, text="Dump", command=lambda:d.dump_event(None))
    dump.pack(side='left')
Пример #16
0
    def create_linenumbers(self):
        """ Create the widget for displaying line numbers. """
        colors = idleConf.GetHighlight(idleConf.CurrentTheme(), 'stdout')
        editwin = self.editwin
        text = self.text
        text_frame = editwin.text_frame
        self.textln = textln = Text(text_frame,
                                    bg=colors['background'],
                                    fg=colors['foreground'],
                                    width=self.width,
                                    height=1,
                                    wrap=NONE,
                                    highlightthickness=0)

        # adjust font
        textln.config(
            font=(idleConf.GetOption('main', 'EditorWindow', 'font'),
                  idleConf.GetOption('main', 'EditorWindow', 'font-size')))

        textln.bind("<FocusIn>", self.focus_in_event)
        textln.bind('<Button-1>', self.button_ignore)
        textln.bind('<Button-2>', self.button_ignore)
        textln.bind('<Button-3>', self.button_ignore)
        textln.bind('<B1-Motion>', self.button_ignore)
        textln.bind('<B2-Motion>', self.button_ignore)
        textln.bind('<B3-Motion>', self.button_ignore)

        textln.bind("<Button-4>", self.button4)
        textln.bind("<Button-5>", self.button5)

        textln.tag_config('LINE', justify=RIGHT)
        textln.insert(END, '1')
        textln.tag_add('LINE', '1.0', END)

        # start the line numbers
        self.per = per = Percolator(textln)
        self.line_delegator = LineDelegator()
        per.insertfilter(self.line_delegator)
        textln._insert = self.line_delegator.delegate.insert
        textln._delete = self.line_delegator.delegate.delete

        lines = LineNumberDelegator(self)
        if _AFTER_UNDO:
            # idlelib.percolator's .insertfilter should have an
            # "after=" argument
            lines.setdelegate(editwin.undo.delegate)
            editwin.undo.setdelegate(lines)
        else:
            editwin.per.insertfilter(lines)

        editwin.vbar['command'] = self.vbar_split
        editwin.text['yscrollcommand'] = self.yscroll_split
Пример #17
0
def _undo_delegator(parent):  # htest #
    import re
    from tkinter import Toplevel, Text, Button
    from idlelib.percolator import Percolator
    undowin = Toplevel(parent)
    undowin.title("Test UndoDelegator")
    width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
    undowin.geometry("+%d+%d" % (x, y + 175))

    text = Text(undowin, height=10)
    text.pack()
    text.focus_set()
    p = Percolator(text)
    d = UndoDelegator()
    p.insertfilter(d)

    undo = Button(undowin, text="Undo", command=lambda: d.undo_event(None))
    undo.pack(side='left')
    redo = Button(undowin, text="Redo", command=lambda: d.redo_event(None))
    redo.pack(side='left')
    dump = Button(undowin, text="Dump", command=lambda: d.dump_event(None))
    dump.pack(side='left')
Пример #18
0
def _undo_delegator(parent):  # htest #
    import re
    import tkinter as tk
    from idlelib.percolator import Percolator
    undowin = tk.Toplevel()
    undowin.title("Test UndoDelegator")
    width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
    undowin.geometry("+%d+%d"%(x, y + 150))

    text = Text(undowin, height=10)
    text.pack()
    text.focus_set()
    p = Percolator(text)
    d = UndoDelegator()
    p.insertfilter(d)

    undo = Button(undowin, text="Undo", command=lambda:d.undo_event(None))
    undo.pack(side='left')
    redo = Button(undowin, text="Redo", command=lambda:d.redo_event(None))
    redo.pack(side='left')
    dump = Button(undowin, text="Dump", command=lambda:d.dump_event(None))
    dump.pack(side='left')
Пример #19
0
    def __init__(self):
        self.root = tkinter.Tk()
        self.key_hook = {'f10': self.create_code}
        self.recorder = recorder(
            outclass=self)  # 这里 outclass 是为了能在 recorder 内的关闭函数中接收关闭信号函数
        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
        self.close_sign = self.on_closing
        self.ft = Font(family='Consolas', size=10)
        Label(self.root, text=info, font=self.ft).pack(padx=5)

        fr = Frame(self.root)
        fr.pack(fill=tkinter.X)
        Label(fr, text='速度 [执行速度,None模式慎用]',
              font=self.ft).pack(side=tkinter.LEFT, padx=5)
        self.cbx = Combobox(fr, width=5, state='readonly')
        self.cbx['values'] = (0.5, 1.0, 1.5, 2.5, 6.5, 17.5, 37.0, 67.5, 115.0,
                              'None')
        self.cbx.current(2)
        self.cbx.pack(side=tkinter.RIGHT)
        self.cbx.bind('<<ComboboxSelected>>', self.change_speed)
        fr = Frame(self.root)
        fr.pack(fill=tkinter.X)
        Label(fr, text='是否区分左右shift [推荐默认否]',
              font=self.ft).pack(side=tkinter.LEFT, padx=5)
        self.cbx2 = Combobox(fr, width=5, state='readonly')
        self.cbx2['values'] = ('否', '是')
        self.cbx2.current(0)
        self.cbx2.pack(side=tkinter.RIGHT)
        self.cbx2.bind('<<ComboboxSelected>>', self.change_shift_diff)
        Button(self.root, text='注意事项',
               command=self.create_warning).pack(fill=tkinter.X)
        Button(self.root, text='生成代码',
               command=self.create_code).pack(fill=tkinter.X)
        self.txt = Text(self.root, width=30, height=11, font=self.ft)
        self.txt.pack(fill=tkinter.BOTH, expand=True)
        global print
        print = self.print
        try:
            from idlelib.colorizer import ColorDelegator
            from idlelib.percolator import Percolator
            p = ColorDelegator()
            Percolator(self.txt).insertfilter(p)
        except:
            traceback.print_exc()
Пример #20
0
    def __init__(self, filename=None):
        self.root = root = turtle._root = Tk()
        root.title('Python turtle-graphics examples')
        root.wm_protocol("WM_DELETE_WINDOW", self._destroy)

        if darwin:
            import subprocess
            # Make sure we are the currently activated OS X application
            # so that our menu bar appears.
            subprocess.run(
                [
                    'osascript',
                    '-e',
                    'tell application "System Events"',
                    '-e',
                    'set frontmost of the first process whose '
                    'unix id is {} to true'.format(os.getpid()),
                    '-e',
                    'end tell',
                ],
                stderr=subprocess.DEVNULL,
                stdout=subprocess.DEVNULL,
            )

        root.grid_rowconfigure(0, weight=1)
        root.grid_columnconfigure(0, weight=1)
        root.grid_columnconfigure(1, minsize=90, weight=1)
        root.grid_columnconfigure(2, minsize=90, weight=1)
        root.grid_columnconfigure(3, minsize=90, weight=1)

        self.mBar = Menu(root, relief=RAISED, borderwidth=2)
        self.mBar.add_cascade(menu=self.makeLoadDemoMenu(self.mBar),
                              label='Examples',
                              underline=0)
        self.mBar.add_cascade(menu=self.makeFontMenu(self.mBar),
                              label='Fontsize',
                              underline=0)
        self.mBar.add_cascade(menu=self.makeHelpMenu(self.mBar),
                              label='Help',
                              underline=0)
        root['menu'] = self.mBar

        pane = PanedWindow(orient=HORIZONTAL,
                           sashwidth=5,
                           sashrelief=SOLID,
                           bg='#ddd')
        pane.add(self.makeTextFrame(pane))
        pane.add(self.makeGraphFrame(pane))
        pane.grid(row=0, columnspan=4, sticky='news')

        self.output_lbl = Label(root,
                                height=1,
                                text=" --- ",
                                bg="#ddf",
                                font=("Arial", 16, 'normal'),
                                borderwidth=2,
                                relief=RIDGE)
        self.start_btn = Button(root,
                                text=" START ",
                                font=btnfont,
                                fg="white",
                                disabledforeground="#fed",
                                command=self.startDemo)
        self.stop_btn = Button(root,
                               text=" STOP ",
                               font=btnfont,
                               fg="white",
                               disabledforeground="#fed",
                               command=self.stopIt)
        self.clear_btn = Button(root,
                                text=" CLEAR ",
                                font=btnfont,
                                fg="white",
                                disabledforeground="#fed",
                                command=self.clearCanvas)
        self.output_lbl.grid(row=1, column=0, sticky='news', padx=(0, 5))
        self.start_btn.grid(row=1, column=1, sticky='ew')
        self.stop_btn.grid(row=1, column=2, sticky='ew')
        self.clear_btn.grid(row=1, column=3, sticky='ew')

        Percolator(self.text).insertfilter(ColorDelegator())
        self.dirty = False
        self.exitflag = False
        if filename:
            self.loadfile(filename)
        self.configGUI(DISABLED, DISABLED, DISABLED,
                       "Choose example from menu", "black")
        self.state = STARTUP
Пример #21
0
 def __init__(self, text):
     self.text = text
     self.text_frame = self.text.master
     self.per = Percolator(text)
     self.undo = Delegator()
     self.per.insertfilter(self.undo)
Пример #22
0
def code_color(text: tk.Text):
    color_config(text)
    p = Percolator(text)
    d = ColorDelegator()
    p.insertfilter(d)
 def setUpClass(cls):
     requires('gui')
     root = cls.root = Tk()
     root.withdraw()
     text = cls.text = Text(root)
     cls.percolator = Percolator(text)
class PercolatorTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.root = Tk()
        cls.text = Text(cls.root)

    @classmethod
    def tearDownClass(cls):
        del cls.text
        cls.root.destroy()
        del cls.root

    def setUp(self):
        self.percolator = Percolator(self.text)
        self.filter_one = MyFilter()
        self.filter_two = MyFilter()
        self.percolator.insertfilter(self.filter_one)
        self.percolator.insertfilter(self.filter_two)

    def tearDown(self):
        self.percolator.close()
        self.text.delete("1.0", END)

    def test_insertfilter(self):
        self.assertIsNotNone(self.filter_one.delegate)
        self.assertEqual(self.percolator.top, self.filter_two)
        self.assertEqual(self.filter_two.delegate, self.filter_one)
        self.assertEqual(self.filter_one.delegate, self.percolator.bottom)

    def test_removefilter(self):
        filter_three = MyFilter()
        self.percolator.removefilter(self.filter_two)
        self.assertEqual(self.percolator.top, self.filter_one)
        self.assertIsNone(self.filter_two.delegate)

        filter_three = MyFilter()
        self.percolator.insertfilter(self.filter_two)
        self.percolator.insertfilter(filter_three)
        self.percolator.removefilter(self.filter_one)
        self.assertEqual(self.percolator.top, filter_three)
        self.assertEqual(filter_three.delegate, self.filter_two)
        self.assertEqual(self.filter_two.delegate, self.percolator.bottom)
        self.assertIsNone(self.filter_one.delegate)

    def test_insert(self):
        self.text.insert("insert", "foo")
        self.assertEqual(self.text.get("1.0", END), "foo\n")
        self.assertTupleEqual(self.filter_one.insert_called_with, ("insert", "foo", None))

    def test_modify_insert(self):
        self.filter_one.insert = self.filter_one.uppercase_insert
        self.text.insert("insert", "bAr")
        self.assertEqual(self.text.get("1.0", END), "BAR\n")

    def test_modify_chain_insert(self):
        filter_three = MyFilter()
        self.percolator.insertfilter(filter_three)
        self.filter_two.insert = self.filter_two.uppercase_insert
        self.filter_one.insert = self.filter_one.lowercase_insert
        self.text.insert("insert", "BaR")
        self.assertEqual(self.text.get("1.0", END), "bar\n")

    def test_dont_insert(self):
        self.filter_one.insert = self.filter_one.dont_insert
        self.text.insert("insert", "foo bar")
        self.assertEqual(self.text.get("1.0", END), "\n")
        self.filter_one.insert = self.filter_one.dont_insert
        self.text.insert("insert", "foo bar")
        self.assertEqual(self.text.get("1.0", END), "\n")

    def test_without_filter(self):
        self.text.insert("insert", "hello")
        self.assertEqual(self.text.get("1.0", "end"), "hello\n")

    def test_delete(self):
        self.text.insert("insert", "foo")
        self.text.delete("1.0", "1.2")
        self.assertEqual(self.text.get("1.0", END), "o\n")
        self.assertTupleEqual(self.filter_one.delete_called_with, ("1.0", "1.2"))
 def setUp(self):
     self.percolator = Percolator(self.text)
     self.filter_one = MyFilter()
     self.filter_two = MyFilter()
     self.percolator.insertfilter(self.filter_one)
     self.percolator.insertfilter(self.filter_two)
Пример #26
0
 def setUp(self):
     self.percolator = Percolator(self.text)
     self.filter_one = MyFilter()
     self.filter_two = MyFilter()
     self.percolator.insertfilter(self.filter_one)
     self.percolator.insertfilter(self.filter_two)
Пример #27
0
class PercolatorTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.root = Tk()
        cls.text = Text(cls.root)

    @classmethod
    def tearDownClass(cls):
        del cls.text
        cls.root.destroy()
        del cls.root

    def setUp(self):
        self.percolator = Percolator(self.text)
        self.filter_one = MyFilter()
        self.filter_two = MyFilter()
        self.percolator.insertfilter(self.filter_one)
        self.percolator.insertfilter(self.filter_two)

    def tearDown(self):
        self.percolator.close()
        self.text.delete('1.0', END)

    def test_insertfilter(self):
        self.assertIsNotNone(self.filter_one.delegate)
        self.assertEqual(self.percolator.top, self.filter_two)
        self.assertEqual(self.filter_two.delegate, self.filter_one)
        self.assertEqual(self.filter_one.delegate, self.percolator.bottom)

    def test_removefilter(self):
        filter_three = MyFilter()
        self.percolator.removefilter(self.filter_two)
        self.assertEqual(self.percolator.top, self.filter_one)
        self.assertIsNone(self.filter_two.delegate)

        filter_three = MyFilter()
        self.percolator.insertfilter(self.filter_two)
        self.percolator.insertfilter(filter_three)
        self.percolator.removefilter(self.filter_one)
        self.assertEqual(self.percolator.top, filter_three)
        self.assertEqual(filter_three.delegate, self.filter_two)
        self.assertEqual(self.filter_two.delegate, self.percolator.bottom)
        self.assertIsNone(self.filter_one.delegate)

    def test_insert(self):
        self.text.insert('insert', 'foo')
        self.assertEqual(self.text.get('1.0', END), 'foo\n')
        self.assertTupleEqual(self.filter_one.insert_called_with,
                              ('insert', 'foo', None))

    def test_modify_insert(self):
        self.filter_one.insert = self.filter_one.uppercase_insert
        self.text.insert('insert', 'bAr')
        self.assertEqual(self.text.get('1.0', END), 'BAR\n')

    def test_modify_chain_insert(self):
        filter_three = MyFilter()
        self.percolator.insertfilter(filter_three)
        self.filter_two.insert = self.filter_two.uppercase_insert
        self.filter_one.insert = self.filter_one.lowercase_insert
        self.text.insert('insert', 'BaR')
        self.assertEqual(self.text.get('1.0', END), 'bar\n')

    def test_dont_insert(self):
        self.filter_one.insert = self.filter_one.dont_insert
        self.text.insert('insert', 'foo bar')
        self.assertEqual(self.text.get('1.0', END), '\n')
        self.filter_one.insert = self.filter_one.dont_insert
        self.text.insert('insert', 'foo bar')
        self.assertEqual(self.text.get('1.0', END), '\n')

    def test_without_filter(self):
        self.text.insert('insert', 'hello')
        self.assertEqual(self.text.get('1.0', 'end'), 'hello\n')

    def test_delete(self):
        self.text.insert('insert', 'foo')
        self.text.delete('1.0', '1.2')
        self.assertEqual(self.text.get('1.0', END), 'o\n')
        self.assertTupleEqual(self.filter_one.delete_called_with,
                              ('1.0', '1.2'))
Пример #28
0
 def setUpClass(cls):
     cls.root = Tk()
     cls.text = Text(cls.root)
     cls.percolator = Percolator(cls.text)
Пример #29
0
def main():
    def convertFile():

        global new_base_dir

        if len(python_2_files) != 0:
            # if ther is a file selected...
            print("\n")

            # convert the files
            for _file in python_2_files:

                base_dir = "/".join(_file.split("/")[:-1])
                new_base_dir = base_dir + "//ConvertedFiles"

                # # create a Converted file and save files there
                # old_dir = os.getcwd()
                # new_dir = old_dir+"//ConvertedFiles"

                if os.path.isdir(new_base_dir) == False:
                    os.makedirs(new_base_dir)
                    print("created the convertedFiles fonder")
                else:
                    pass

                # change the working directory to Converted folder
                os.chdir(new_base_dir)

                # cpoy the file to be conerted to the convertedFiles path
                new__file_path = os.getcwd() + "/" + os.path.basename(_file)
                x = shutil.copy(_file, new__file_path)
                # convert each selected python 2 file
                command = [py2to3pPath, "-w", "-n", x]

                run(command)
                print("done, converting : ", os.path.basename(_file))
                print("command = ", command)

            print("Done all operations")

            python3SrcBtn.config(state=NORMAL)
            py3Dest.set(new_base_dir)

    def searchFile():

        global python_2_files

        files_list = filedialog.askopenfilenames(
            filetypes=[("Python Files .py",
                        ".py"), ("Python .pyw files",
                                 ".pyw"), ("All Files", "*")])
        # print(files_list)

        files = []
        for i in range(len(files_list)):
            x = os.path.basename(files_list[i])
            files.append(x)

        python_2_files = files_list
        files = ", ".join(files)
        py2file.set(files)
        print(files)
        print(python_2_files)

    def openPy3Destination():

        # dest = os.open(new_base_dir)
        webbrowser.open(new_base_dir)

        # print(dest)

    def convertText():

        py2Txt = python2Text.get(0.1, END)

        with open("utils\\py2.py", "w") as py2TextOb:
            py2TextOb.write(py2Txt)

        command = ["utils\\2to3.exe", "-w", "-W", "-n", "utils\\py2.py"]

        run(command)

        # print(command)

        with open("utils\\py2.py", "r") as py3TextOb:
            py2Txt = py3TextOb.read()
            python3Text.delete(0.1, END)
            python3Text.insert(0.1, py2Txt)

    root = Tk()
    root.title("Py2-to-Py3-Converter-GUI-tkinter")
    root.geometry("800x550")
    root.resizable(width=0, height=0)
    root.grid_rowconfigure(0, weight=1)  # this needed to be added
    root.grid_columnconfigure(0, weight=1)  # as did this

    #  TKINTER VARIABLES
    py2file = StringVar()
    py2file.set("")
    py3Dest = StringVar()
    py3Dest.set("")

    # the Notebook
    notebook = ttk.Notebook(root, padding=5)
    notebook.grid(sticky="nsew")
    notebook.grid_rowconfigure(0, weight=1)  # this needed to be added
    notebook.grid_columnconfigure(0, weight=1)  # as did this

    # file Converter tab
    filesConverterFrame = Frame(root)
    filesConverterFrame.grid(row=0, column=0, sticky="nsew")
    filesConverterFrame.grid_rowconfigure(0,
                                          weight=1)  # this needed to be added
    filesConverterFrame.grid_columnconfigure(0, weight=1)  # as did this
    fileConvCanv = Canvas(filesConverterFrame)
    fileConvCanv.grid(sticky="nsew")
    fileConvCanv.grid_rowconfigure(1, pad=60)
    fileConvCanv.grid_rowconfigure(2, pad=50)
    fileConvCanv.grid_rowconfigure(3, pad=50)  # this needed to be added
    fileConvCanv.grid_columnconfigure(1, pad=50)  # as did this

    python2Label = Label(fileConvCanv, text="Python2File:", font=LABELFONT)
    python2Label.grid(row=1, column=1)

    python3Label = Label(fileConvCanv,
                         text="Py3FileDirectory:",
                         font=LABELFONT)
    python3Label.grid(row=2, column=1)

    python2Entry = Entry(fileConvCanv,
                         font=ENTRYFONT,
                         width=60,
                         textvariable=py2file)
    python2Entry.grid(row=1, column=2, sticky="w")

    python3Entry = Entry(fileConvCanv,
                         font=ENTRYFONT,
                         width=60,
                         textvariable=py3Dest)
    python3Entry.grid(row=2, column=2, sticky="w")

    python2SrcBtn = Button(fileConvCanv,
                           text="Search",
                           font=BUTTONFONT,
                           command=searchFile)
    python2SrcBtn.grid(row=1, column=3, padx=5)

    python3SrcBtn = Button(fileConvCanv,
                           text="Open",
                           font=BUTTONFONT,
                           command=openPy3Destination,
                           state=findState)
    python3SrcBtn.grid(row=2, column=3)

    convertBtn = Button(fileConvCanv,
                        text="CONVERT",
                        font=BUTTONFONT,
                        command=convertFile)
    convertBtn.grid(row=3, column=2)

    # settingsBtn = Button(fileConvCanv, text="SS")
    # settingsBtn.grid(row=50, column=4, sticky="se")

    # raw text converter tab
    rawTextConverterFrame = Frame(bg="green")
    rawTextConverterFrame.grid(row=0, column=0, sticky="nsew")
    rawTextConverterFrame.grid_rowconfigure(
        0, weight=1)  # this needed to be added
    rawTextConverterFrame.grid_columnconfigure(0, weight=1)  # as did this
    rawTextConvCanv = Canvas(rawTextConverterFrame)
    rawTextConvCanv.grid(sticky="nsew")

    python2TextLabel = Label(rawTextConvCanv, text="Python 2 Code...")
    python2TextLabel.grid(row=1, column=1, padx=5, pady=5)

    python2Text = Text(rawTextConvCanv, width=43, wrap="none")
    Percolator(python2Text).insertfilter(ColorDelegator())
    color_config(python2Text)
    python2Text.grid(row=2, column=1, padx=5)
    # Python 3 text scrollbar
    hbar = Scrollbar(rawTextConvCanv, name='hbar', orient=HORIZONTAL)
    hbar['command'] = python2Text.xview
    hbar.grid(row=50, column=1, sticky="ew", padx=2)
    python2Text['xscrollcommand'] = hbar.set

    toTextLabel = Label(rawTextConvCanv, text=">>>")
    toTextLabel.grid(row=2, column=2, sticky="nsew")

    python3TextLabel = Label(rawTextConvCanv, text="Python 3 Code")
    python3TextLabel.grid(row=1, column=3)

    python3Text = Text(rawTextConvCanv, width=43, wrap="none")
    python3Text.insert(0.1, "print(\"Hello world\")")
    Percolator(python3Text).insertfilter(ColorDelegator())
    color_config(python3Text)
    python3Text.grid(row=2, column=3)
    # Python 3 text scrollbar
    hbar1 = Scrollbar(rawTextConvCanv, name='hbar1', orient=HORIZONTAL)
    hbar1['command'] = python3Text.xview
    hbar1.grid(row=50, column=3, sticky="ew")
    python3Text['xscrollcommand'] = hbar1.set

    convertBtnText = Button(rawTextConvCanv,
                            text="CONVERT",
                            command=convertText)
    convertBtnText.grid(row=3, column=2, pady=10, padx=5)

    # Notebook Adding of Tabs
    notebook.add(rawTextConverterFrame, text="Convert Raw python code")
    notebook.add(filesConverterFrame, text="Convert Files")

    notebook.select(".!frame")

    # print(notebook.tabs())

    root.mainloop()
v = re.findall('\d+','asdf909080asdf8908asdf980890asdf')
print(v)
v = requests.get('http://www.baidu.com')
print(v)

# for i in range(10):
#     time.sleep(.5)
#     print(i)
'''
code.insert(0., mycode)

# ==== 使用 idlelib 自带的语法高亮 ====
from idlelib.colorizer import ColorDelegator
from idlelib.percolator import Percolator
d = ColorDelegator()
Percolator(code).insertfilter(d)

mlog = tkinter.Text(root)
mlog.pack()
mlog.insert(0., mycode)

highlight_style = {
    'COMMENT': {
        'foreground': '#0000dd',
        'background': '#ffffff'
    },
    'KEYWORD': {
        'foreground': '#ff77dd',
        'background': '#ffffff'
    },
    'BUILTIN': {
Пример #31
0
    def 建立文字及繪圖區(self):
        """
        建立兩個Text區跟龜模組畫圖區並建立快捷鍵        
        """
        setup = {}
        pane = PanedWindow(orient=HORIZONTAL, bg='black')
        text_frame = Frame(pane)
        self.text = text = Text(text_frame,
                                name='text',
                                padx=5,
                                wrap='none',
                                width=45,
                                bg='white',
                                tabs="1" * 4,
                                font=tuple(txtfont),
                                undo=True)
        text.name = "text"
        vbar = Scrollbar(text_frame, name='vbar')

        vbar['command'] = text.yview
        vbar.pack(side=RIGHT, fill=Y)
        hbar = Scrollbar(text_frame, name='hbar', orient=HORIZONTAL)
        hbar['command'] = text.xview
        hbar.pack(side=BOTTOM, fill=X)
        text['yscrollcommand'] = vbar.set
        text['xscrollcommand'] = hbar.set
        text.pack(side=LEFT, fill=BOTH, expand=1)

        pane.add(text_frame)

        text_frame2 = Frame(pane)
        self.text2 = text2 = Text(text_frame2,
                                  name='text',
                                  padx=5,
                                  wrap='none',
                                  width=45,
                                  bg='white',
                                  tabs="1",
                                  font=tuple(txtfont),
                                  undo=True)
        text.name = "text2"
        vbar = Scrollbar(text_frame2, name='vbar')
        vbar['command'] = text2.yview
        vbar.pack(side=RIGHT, fill=Y)
        hbar = Scrollbar(text_frame2, name='hbar', orient=HORIZONTAL)
        hbar['command'] = text2.xview
        hbar.pack(side=BOTTOM, fill=X)
        text2['yscrollcommand'] = vbar.set
        text2['xscrollcommand'] = hbar.set
        text2.pack(side=LEFT, fill=BOTH, expand=1)
        pane.add(text_frame2)

        self.canvwidth = 800
        self.canvheight = 600
        turtle._Screen._root = self.root
        turtle._Screen._canvas = canvas = self._canvas = turtle.ScrolledCanvas(
            self.root, 800, 600, 1000, 800)
        canvas.adjustScrolls()
        canvas._rootwindow.bind('<Configure>', self.onResize)
        canvas._canvas['borderwidth'] = 0
        self.screen = screen = turtle.Screen()
        turtle.TurtleScreen.__init__(screen, screen._canvas)
        self.scanvas = scanvas = screen._canvas
        turtle.RawTurtle.screens = [screen]
        canvas.pack(side=LEFT, fill=BOTH, expand=1)
        pane.add(canvas)  # for 畫圖
        # self.scanvas.bind('<Button-1>',self.焦點轉移)

        pane.pack(side=TOP, expand=1, fill=BOTH)

        Percolator(text).insertfilter(ColorDelegator())
        Percolator(text2).insertfilter(ColorDelegator())
        # text.bind("<Tab>", self.tab)
        # text2.bind("<Tab>", self.tab)
        text.bind('<Control-MouseWheel>', self.滑鼠滾輪)
        text.bind('<Control-Button-4>', self.字體放大)
        text.bind('<Control-Button-5>', self.字體縮小)
        text2.bind('<Control-MouseWheel>', self.滑鼠滾輪)
        text2.bind('<Control-Button-4>', self.字體放大)
        text2.bind('<Control-Button-5>', self.字體縮小)
        text.bind('<Button-3>', self.選取標記)
        text2.bind('<Button-3>', self.選取標記)
        text.bind("<KeyRelease>", self.行號更新)
        text.bind("<ButtonRelease>", self.行號更新)
        text2.bind("<KeyRelease>", self.行號更新)
        text2.bind("<ButtonRelease>", self.行號更新)
Пример #32
0
class PercolatorTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.root = Tk()
        cls.text = Text(cls.root)

    @classmethod
    def tearDownClass(cls):
        del cls.text
        cls.root.destroy()
        del cls.root

    def setUp(self):
        self.percolator = Percolator(self.text)
        self.filter_one = MyFilter()
        self.filter_two = MyFilter()
        self.percolator.insertfilter(self.filter_one)
        self.percolator.insertfilter(self.filter_two)

    def tearDown(self):
        self.percolator.close()
        self.text.delete('1.0', END)

    def test_insertfilter(self):
        self.assertIsNotNone(self.filter_one.delegate)
        self.assertEqual(self.percolator.top, self.filter_two)
        self.assertEqual(self.filter_two.delegate, self.filter_one)
        self.assertEqual(self.filter_one.delegate, self.percolator.bottom)

    def test_removefilter(self):
        filter_three = MyFilter()
        self.percolator.removefilter(self.filter_two)
        self.assertEqual(self.percolator.top, self.filter_one)
        self.assertIsNone(self.filter_two.delegate)
        filter_three = MyFilter()
        self.percolator.insertfilter(self.filter_two)
        self.percolator.insertfilter(filter_three)
        self.percolator.removefilter(self.filter_one)
        self.assertEqual(self.percolator.top, filter_three)
        self.assertEqual(filter_three.delegate, self.filter_two)
        self.assertEqual(self.filter_two.delegate, self.percolator.bottom)
        self.assertIsNone(self.filter_one.delegate)

    def test_insert(self):
        self.text.insert('insert', 'foo')
        self.assertEqual(self.text.get('1.0', END), 'foo\n')
        self.assertTupleEqual(self.filter_one.insert_called_with,
                              ('insert', 'foo', None))

    def test_modify_insert(self):
        self.filter_one.insert = self.filter_one.uppercase_insert
        self.text.insert('insert', 'bAr')
        self.assertEqual(self.text.get('1.0', END), 'BAR\n')

    def test_modify_chain_insert(self):
        filter_three = MyFilter()
        self.percolator.insertfilter(filter_three)
        self.filter_two.insert = self.filter_two.uppercase_insert
        self.filter_one.insert = self.filter_one.lowercase_insert
        self.text.insert('insert', 'BaR')
        self.assertEqual(self.text.get('1.0', END), 'bar\n')

    def test_dont_insert(self):
        self.filter_one.insert = self.filter_one.dont_insert
        self.text.insert('insert', 'foo bar')
        self.assertEqual(self.text.get('1.0', END), '\n')
        self.filter_one.insert = self.filter_one.dont_insert
        self.text.insert('insert', 'foo bar')
        self.assertEqual(self.text.get('1.0', END), '\n')

    def test_without_filter(self):
        self.text.insert('insert', 'hello')
        self.assertEqual(self.text.get('1.0', 'end'), 'hello\n')

    def test_delete(self):
        self.text.insert('insert', 'foo')
        self.text.delete('1.0', '1.2')
        self.assertEqual(self.text.get('1.0', END), 'o\n')
        self.assertTupleEqual(self.filter_one.delete_called_with,
                              ('1.0', '1.2'))