title: How to create a console inside my code editor which runs python code in python tkinter?
body: <p>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</p>
  <p>This is my run function:-</p>
  <pre><code>def run(event=None):
      global file
      if file == &quot;&quot;:
          pass
      else:
          
          command = f&quot;python {file}&quot;
          run_file = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
          Output, error = run_file.communicate()
          output.insert(END,f&quot;{file}&gt;&gt;\n&quot;)
          output.insert(END,Output)
          output.insert(END,error)
  </code></pre>
  <p>this is the error:-</p>
  <pre><code>C:\Users\USER\AppData\Local\Programs\Python\Python310\python.exe: can't find '__main__' module in 'D:\\coding'
  
  </code></pre>
  <p>full code (not full code short version of real code focusing only my problem):-</p>
  <pre><code>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 &quot;values&quot; 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(&quot;&quot;,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,&quot;values&quot;)[0]
      if os.path.isfile(file):
          editor.delete(1.0,END)
          with open(file,&quot;r&quot;) as f:
              editor.insert(1.0,f.read())
  def save(event=None):
      global file
      if file == &quot;&quot;:
          saveas()
      else:
          item = tree.selection()
      # get the absolute path
          file = tree.item(item,&quot;values&quot;)[0]
          filepath = os.path.join(path,file)
          with open(file,&quot;w&quot;) as f:
              f.write(editor.get(1.0,END))
          root.title(os.path.basename(file) + &quot;-Python&quot;)
  def saveas(event=None):
      global file
      file = asksaveasfilename(defaultextension=&quot;.py&quot;,filetypes=[(&quot;Python Files&quot;,&quot;*.py&quot;)])
      if file == &quot;&quot;:
          file = None
      else:
          with open(file,&quot;w&quot;) as f:
              f.write(editor.get(1.0,END))
          root.title(os.path.basename(file) + &quot;-Python&quot;)
  def run(event=None):
      global file
      if file == &quot;&quot;:
          pass
      else:
          
          command = f&quot;python {file}&quot;
          run_file = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
          Output, error = run_file.communicate()
          output.insert(END,f&quot;{file}&gt;&gt;\n&quot;)
          output.insert(END,Output)
          output.insert(END,error)
  
  root = Tk()
  tree = ttk.Treeview()
  tree.pack(side=LEFT,fill=BOTH)
  file = &quot;&quot;
  path = &quot;&quot;
  style = ttk.Style()
  style.theme_use(&quot;clam&quot;)
  style.configure(&quot;Treeview&quot;,background=&quot;gray19&quot;,foreground=&quot;white&quot;,fieldbackground=&quot;gray19&quot;)
  editor = Text(font=&quot;Consolas 15&quot;,bg=&quot;gray19&quot;,fg=&quot;white&quot;,insertbackground=&quot;white&quot;)
  editor.pack(expand=True,fill=BOTH)
  output = Text(height=15,font=&quot;Consolas 15&quot;,bg=&quot;gray19&quot;,fg=&quot;white&quot;,insertbackground=&quot;white&quot;,insertwidth=10)
  output.pack(expand=True,fill=BOTH)
  
  
  
  root.bind(&quot;&lt;Control-Alt-o&gt;&quot;,Open)
  root.bind(&quot;&lt;Control-s&gt;&quot;,save)
  root.bind(&quot;&lt;Control-Alt-s&gt;&quot;,saveas)
  root.bind(&quot;&lt;Shift-Return&gt;&quot;,run)
  tree.bind(&quot;&lt;&lt;TreeviewSelect&gt;&gt;&quot;,select_file)
  
  
  
  
  
  
  root.mainloop()
  
  </code></pre>
  <p>This is the image of my code editor that I have created:-
  <a href="https://i.stack.imgur.com/FmJkt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FmJkt.jpg" alt="enter image description here" /></a></p>