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 = []
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()
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()
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.行號更新)
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
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' },
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()
def code_color(text: tk.Text): color_config(text) p = Percolator(text) d = ColorDelegator() p.insertfilter(d)