Movatterモバイル変換


[0]ホーム

URL:



Code forHow to Make a Python Code Editor using Tkinter in Python Tutorial


View on Github

python_code_editor.py

from tkinter import *import ctypesimport reimport os# Increas Dots Per inch so it looks sharperctypes.windll.shcore.SetProcessDpiAwareness(True)# Setup Tkinterroot = Tk()root.geometry('500x500')# Execute the Programmdef execute(event=None):    # Write the Content to the Temporary File    with open('run.py', 'w', encoding='utf-8') as f:        f.write(editArea.get('1.0', END))    # Start the File in a new CMD Window    os.system('start cmd /K "python run.py"')# Register Changes made to the Editor Contentdef changes(event=None):    global previousText    # If actually no changes have been made stop / return the function    if editArea.get('1.0', END) == previousText:        return    # Remove all tags so they can be redrawn    for tag in editArea.tag_names():        editArea.tag_remove(tag, "1.0", "end")    # Add tags where the search_re function found the pattern    i = 0    for pattern, color in repl:        for start, end in search_re(pattern, editArea.get('1.0', END)):            editArea.tag_add(f'{i}', start, end)            editArea.tag_config(f'{i}', foreground=color)            i+=1    previousText = editArea.get('1.0', END) def search_re(pattern, text, groupid=0):    matches = []    text = text.splitlines()    for i, line in enumerate(text):        for match in re.finditer(pattern, line):            matches.append(                (f"{i + 1}.{match.start()}", f"{i + 1}.{match.end()}")            )    return matchesdef rgb(rgb):    return "#%02x%02x%02x" % rgbpreviousText = ''# Define colors for the variouse types of tokensnormal = rgb((234, 234, 234))keywords = rgb((234, 95, 95))comments = rgb((95, 234, 165))string = rgb((234, 162, 95))function = rgb((95, 211, 234))background = rgb((42, 42, 42))font = 'Consolas 15'# Define a list of Regex Pattern that should be colored in a certain wayrepl = [    ['(^| )(False|None|True|and|as|assert|async|await|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|while|with|yield)($| )', keywords],    ['".*?"', string],    ['\'.*?\'', string],    ['#.*?$', comments],]# Make the Text Widget# Add a hefty border width so we can achieve a little bit of paddingeditArea = Text(    root,    background=background,    foreground=normal,    insertbackground=normal,    relief=FLAT,    borderwidth=30,    font=font)# Place the Edit Area with the pack methodeditArea.pack(    fill=BOTH,    expand=1)# Insert some Standard Text into the Edit AreaeditArea.insert('1.0', """from argparse import ArgumentParserfrom random import shuffle, choiceimport string# Setting up the Argument Parserparser = ArgumentParser(    prog='Password Generator.',    description='Generate any number of passwords with this tool.')""")# Bind the KeyRelase to the Changes FunctioneditArea.bind('<KeyRelease>', changes)# Bind Control + R to the exec functionroot.bind('<Control-r>', execute)changes()root.mainloop()

Mastering YOLO - Topic - Top


Join 50,000+ Python Programmers & Enthusiasts like you!



Tags

Practical Python PDF Processing EBook - Contact Us - Middle


New Tutorials

Popular Tutorials


Practical Python PDF Processing EBook - Tutorials - Bottom







[8]ページ先頭

©2009-2025 Movatter.jp