Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter.filedialog import (
- asksaveasfilename,
- askopenfilename
- )
- from tkinter.font import Font
- from tkinter.messagebox import askyesno
- import subprocess
- buildin_keywords = (
- "import", "print", "list", "dict", "for", "in",
- "or", "and", "not", "tuple", "set"
- )
- tab_size = 0
- global_filepath = ''
- shortcuts_map = {
- '<Control-s>': lambda event: save_as(),
- '<Control-o>': lambda event: open_file(),
- '<Control-n>': lambda event: create_newfile(),
- '<Control-q>': lambda event: compiler.destroy(),
- '<Control-b>': lambda event: run(),
- '<Control-a>': lambda event: select_all()
- }
- def open_outputscr():
- output_scr = tk.Toplevel(compiler)
- output_scr.title("OUTPUT")
- output_scr.minsize(800, 500)
- def run():
- global global_filepath
- output_window = tk.Toplevel(compiler)
- vsbar1 = tk.Scrollbar(output_window, orient="vertical")
- code_output = tk.Text(output_window, height=10, yscrollcommand=vsbar1.set, fg="#000000", bg="gray")
- vsbar1.pack(side=tk.RIGHT, fill='y')
- vsbar1.config(command=code_output.yview)
- code_output.configure(state=tk.DISABLED)
- code_output.pack()
- code_output.configure(state=tk.NORMAL)
- code_output.delete('1.0', tk.END)
- if global_filepath == '':
- ans = askyesno(title="File Not Saved", message="Do you want to save file?")
- if ans:
- save_as()
- else:
- return
- save_as()
- command = f'python {global_filepath}'
- process = subprocess.Popen(
- command,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- shell=True
- )
- output, error = process.communicate()
- code_output.insert('1.0', output)
- code_output.insert('1.0', error)
- code_output.configure(state=tk.DISABLED)
- output_window.bind('<Control-q>', lambda event: output_window.destroy())
- def save_as():
- global global_filepath
- if global_filepath == '':
- file_path = asksaveasfilename(
- filetypes= [('Python Files', '*.py')],
- initialdir='/storage/emulated/0' ,
- )
- else:
- file_path = global_filepath
- if file_path:
- with open(file_path, 'w') as file:
- file.write(editor.get('1.0', tk.END))
- global_filepath = file_path
- def open_file():
- global global_filepath
- file_path = askopenfilename(
- filetypes= [('Python Files', '*.py')],
- initialdir='/storage/emulated/0' ,
- )
- if file_path:
- with open(file_path, 'r') as file:
- editor.delete('1.0', tk.END)
- editor.insert('1.0', file.read())
- global_filepath = file_path
- def create_newfile():
- compiler.withdraw()
- compiler.deiconify()
- editor.delete('1.0', tk.END)
- def select_all():
- editor.tag_add(tk.SEL, "1.0", tk.END)
- editor.mark_set(tk.INSERT, "1.0")
- editor.see(tk.INSERT)
- return 'break'
- def get_cursorpos():
- return editor.index(tk.INSERT).split('.')
- #autocompletes
- def symbol_autocomplete(e):
- if str(e.widget) != '.!text':
- return
- brackets = {'(':')', '{':'}', '[':']', '\'':'\'', '\"':'\"'}
- if e.char in brackets:
- editor.insert(editor.index(tk.INSERT), brackets[e.char])
- linecol_sep = editor.index(tk.INSERT)
- sep_idx = linecol_sep.find('.')
- editor.mark_set(
- tk.INSERT,
- f"{linecol_sep[0:int(sep_idx)]}.{int(linecol_sep[int(sep_idx)+1:])-1}")
- compiler = tk.Tk()
- compiler.title('RUDY COMPILER')
- compiler.bind('<Key>', symbol_autocomplete)
- #shortcuts binding
- for key, action in shortcuts_map.items():
- compiler.bind(key, action)
- #scrollbar in editor
- vsbar = tk.Scrollbar(compiler, orient="vertical")
- #editor
- editor = tk.Text(compiler, yscrollcommand=vsbar.set, height=15, bg="#000000", fg="lightgreen")
- vsbar.pack(side=tk.RIGHT, fill='y')
- vsbar.config(command=editor.yview)
- editor.focus_set()
- editor.config(insertbackground="#FFF", tabs=Font().measure(' '))
- editor.pack(expand=True, fill="both")
- #main menu
- menu_bar = tk.Menu(compiler, background="#1B1212", foreground="#fff")
- compiler.config(menu=menu_bar, bg="#000")
- #file menu
- file_menu = tk.Menu(menu_bar)
- file_menu.add_command(label = 'Open', command = open_file)
- file_menu.add_command(label="New", command=create_newfile)
- file_menu.add_command(label='Save', command=save_as)
- file_menu.add_command(label='Save as', command=save_as)
- file_menu.add_command(label='Exit',command=lambda: compiler.destroy())
- #run menu
- run_menu = tk.Menu(menu_bar)
- run_menu.add_command(label="Run", command=run)
- #cascade
- menu_bar.add_cascade(label = 'File', menu=file_menu)
- menu_bar.add_cascade(label="Run", menu=run_menu)
- compiler.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement