'How to create a console inside my code editor which runs python code in python tkinter?
I am creating an code editor and my run function is unable to run python script that's why I want to create a console like terminal like other modern code editor have How to create a console like terminal for python code editor
This is my run function:-
def run(event=None):
global file
if file == "":
pass
else:
command = f"python {file}"
run_file = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
Output, error = run_file.communicate()
output.insert(END,f"{file}>>\n")
output.insert(END,Output)
output.insert(END,error)
this is the error:-
C:\Users\USER\AppData\Local\Programs\Python\Python310\python.exe: can't find '__main__' module in 'D:\\coding'
full code (not full code short version of real code focusing only my problem):-
import os
import subprocess
from tkinter import*
from tkinter import ttk
from tkinter.filedialog import askdirectory, askopenfilename, asksaveasfilename
def process_directory(parent,path):
for i in os.listdir(path):
abspath = os.path.join(path,i)
dirv = os.path.isdir(abspath)
# save the absolute path in "values" option
oid = tree.insert(parent,END,text=i,open=False,values=(abspath,))
if dirv:
process_directory(oid,abspath)
def Open(event=None):
global path
for i in tree.get_children():
tree.delete(i)
path = askdirectory()
abspath = os.path.abspath(path)
root_node = tree.insert("",END,text=abspath,open=True)
process_directory(root_node,abspath)
def select_file(event=None):
global file
item = tree.selection()
# get the absolute path
file = tree.item(item,"values")[0]
if os.path.isfile(file):
editor.delete(1.0,END)
with open(file,"r") as f:
editor.insert(1.0,f.read())
def save(event=None):
global file
if file == "":
saveas()
else:
item = tree.selection()
# get the absolute path
file = tree.item(item,"values")[0]
filepath = os.path.join(path,file)
with open(file,"w") as f:
f.write(editor.get(1.0,END))
root.title(os.path.basename(file) + "-Python")
def saveas(event=None):
global file
file = asksaveasfilename(defaultextension=".py",filetypes=[("Python Files","*.py")])
if file == "":
file = None
else:
with open(file,"w") as f:
f.write(editor.get(1.0,END))
root.title(os.path.basename(file) + "-Python")
def run(event=None):
global file
if file == "":
pass
else:
command = f"python {file}"
run_file = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
Output, error = run_file.communicate()
output.insert(END,f"{file}>>\n")
output.insert(END,Output)
output.insert(END,error)
root = Tk()
tree = ttk.Treeview()
tree.pack(side=LEFT,fill=BOTH)
file = ""
path = ""
style = ttk.Style()
style.theme_use("clam")
style.configure("Treeview",background="gray19",foreground="white",fieldbackground="gray19")
editor = Text(font="Consolas 15",bg="gray19",fg="white",insertbackground="white")
editor.pack(expand=True,fill=BOTH)
output = Text(height=15,font="Consolas 15",bg="gray19",fg="white",insertbackground="white",insertwidth=10)
output.pack(expand=True,fill=BOTH)
root.bind("<Control-Alt-o>",Open)
root.bind("<Control-s>",save)
root.bind("<Control-Alt-s>",saveas)
root.bind("<Shift-Return>",run)
tree.bind("<<TreeviewSelect>>",select_file)
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

