-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (105 loc) · 3.48 KB
/
main.py
File metadata and controls
144 lines (105 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import tkinter as tk
from tkinter import messagebox
import sys # when i import it, my heart beats
import traceback
engine = tk.Tk()
engine.geometry("1024x720") # valid, failed?
engine.title("Micro Media Engine 0.0.1v prototype")
class TkConsoleIO:
def __init__(self, text_widget: tk.Text):
self.text = text_widget
def write(self, s: str):
def _append():
self.text.configure(state="normal")
self.text.insert("end", s)
self.text.see("end")
self.text.configure(state="disabled")
self.text.after(0, _append)
def flush(self):
pass
def run_code(): # its harder than i thought
code = textbox.get("1.0", "end-1c")
try:
old_out, old_err = sys.stdout, sys.stderr
sys.stdout = console_io
sys.stderr = console_io
console_io.write("\n* ~> script runned\n")
exec(code, {})
console_io.write("* ~> script done!\n")
except Exception as e:
console_io.write("*y~> script runtime error\n")
console_io.write(traceback.format_exc() + "\n")
console_io.write("*e~> script error\n => " + str(e))
finally:
try:
sys.stdout = old_out
sys.stderr = old_err
except Exception:
pass
def cruel_world(): # GOODBYE CRUEL WORLD...
print("nullnullnull") # cruel but valid
def new_file():
textbox.delete("1.0", "end")
def set_text(text): # pretty useless function!
textbox.delete("1.0", tk.END) # gosh why here? between dev_lib_1 and 2???? who am i????
textbox.insert("1.0", text) # oh fixed!
def dev_lib0():
devlib0 = """
# script :: devlib0
print("hello, world!")
# Credit : Florian Müller
"""
set_text(devlib0)
def dev_lib1():
devlib1 = """
# script :: devlib1
import random
print("your final number is : " + str(random.randint(0, 16)))
# Credit : Florian Müller
"""
set_text(devlib1)
def dev_lib2():
devlib2 = """
# script :: devlib2
from nullbox.msgbox import *
msg_info("Micro Media Engine", "> Set Text Here <")
# Credit : Florian Müller, Walter Müller
"""
set_text(devlib2)
menubar = tk.Menu(engine)
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Engine", command=cruel_world, accelerator="Ctrl+Shift+E")
file_menu.add_command(label="Run", command=run_code, accelerator="Ctrl+R")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=engine.quit, accelerator="Shift+Q")
menubar.add_cascade(label="File", menu=file_menu)
presets = tk.Menu(menubar, tearoff=0)
presets.add_command(label="Print", command=dev_lib0)
presets.add_command(label="Random", command=dev_lib1)
presets.add_command(label="Visual", command=dev_lib2)
menubar.add_cascade(label="Presets", menu=presets)
engine.config(menu=menubar)
textbox = tk.Text(
engine,
font=("Cascadia Mono", 12),
undo=True
)
textbox.pack(padx=10, pady=10, fill="both", expand=True)
console_frame = tk.Frame(engine)
console_frame.pack(padx=10, pady=(0, 10), fill="x")
console_label = tk.Label(console_frame, text="Console")
console_label.pack(anchor="w")
console = tk.Text(
console_frame,
height=10,
font=("Cascadia Mono", 10),
wrap="word"
)
console.pack(fill="x")
console.configure(state="disabled")
console_io = TkConsoleIO(console)
engine.bind("<Control-r>", lambda e: run_code())
engine.bind("<Control-Shift-E>", lambda e: cruel_world())
engine.bind("<Shift-Q>", lambda e: engine.quit())
engine.mainloop()