I’m are creating a programming language called ‘procol’ with basic syntax. In this language, you want to create a functionality where a variable can be set to the function inco(”), which will open a new window with a prompt and a text bar. The user’s input in the prompt should then set the value of that variable. However, I’m facing difficulties in implementing this feature. I’m seeking assistance to resolve this issue. For extra information, I’m using the tkinter module. Heres the code(I just started on it so there’s lack of currently usable functions):
import tkinter as tk
variables = {}
def inco(prompt, variable_name):
root = tk.Tk()
def on_submit(prompt, variable_name):
user_input = entry.get()
variables[variable_name] = user_input
root.destroy()
label = tk.Label(root, text=prompt)
label.pack()
entry = tk.Entry(root)
entry.pack()
submit_button = tk.Button(root, text="Submit", command=lambda: on_submit(prompt, variable_name))
submit_button.pack()
root.mainloop()
def run_program():
code = code_input.get('1.0', 'end-1c')
output = ''
lines = code.split('n')
for line in lines:
line = line.strip()
if not line:
continue
elif '=' in line:
data_type, rest = line.split(maxsplit=1)
variable_name, value = rest.split('=', 1)
variable_name = variable_name.strip()
value = value.strip()
if 'inco(' in value:
prompt = value.split("inco(")[1].split(")")[0]
inco(prompt, variable_name)
else:
if data_type == 'int':
variables[variable_name] = int(value)
elif data_type == 'dbl':
variables[variable_name] = float(value)
elif data_type == 'str':
variables[variable_name] = value
elif 'disco(' in line:
expression = line.split("disco(")[1].split(")")[0]
result = eval(expression, {}, variables)
output += str(result) + 'n'
output_text.set(output)
root = tk.Tk()
root.title("Procol Compiler 1.0")
code_input = tk.Text(root, wrap=tk.WORD)
code_input.pack(fill=tk.BOTH, expand=True)
output_text = tk.StringVar()
output_label = tk.Label(root, textvariable=output_text, wraplength=400)
output_label.pack()
run_button = tk.Button(root, text="Run", command=run_program)
run_button.pack(side=tk.LEFT)
root.mainloop()