0
import sys
from tkinter import *
import time

class main:

    def __init__(self, text=""):
        self.root = Tk()
        self.root.title("python editor")
        self.Text = text
        self.window = self.text_window(self, self.Text)
        self.root.mainloop()

    class text_window:

        def __init__(self, master, text):
            self.master = master

            self.textBox = Text(master.root)
            self.textBox.insert(END, text)
            self.textBox.grid(row=0, sticky=NSEW, columnspan=2)
            self.master.root.rowconfigure(0, weight=1)
            self.master.root.columnconfigure(0, weight=1)
            self.buttonOK = Button(master.root, text="run",
                command=self.get_text)
            self.buttonOK.bind('Enter',self.get_text)
            self.buttonOK.grid(row=1, column=2)
            self.buttonNO=Button(master.root, text="close window",command=self.abort)
            self.buttonNO.grid(row=1, column=1)


        def get_text(self):
            text= self.textBox.get(1.0, END)
            if ">>> " in text:
                qwerty=str(exec(text[4:]))
                return qwerty
            else:
                qwerty=exec(text)
                return qwerty


        def abort(self):
            sys.exit()

def get_main(text=""):
    m = main(text)
    output = m.Text
    return output

get_main(">>> ")

I am trying to make a tkinter window that will run multiline code like IDLE. Everything other than loops work.

If I try to run an infinite loop (While loop), and tried to abort it, it doesn't abort and just runs it forever until the window says (not responding).

I am using python 3.6 on windows 10.

Nae
  • 14,209
  • 7
  • 52
  • 79
  • `While True` loops and tkinter don't play nice together. Tkinter runs its own `While True` loop to display GUI, you can either go multi-threading or make use of event queue using `after`. Also, see https://stackoverflow.com/q/665566/7032856. – Nae Jan 21 '18 at 16:43
  • what is multi-threading? – iwasnothere Jan 21 '18 at 16:50
  • Sorry, I'm not very knowledgeable on the subject, but it should roughly be the same concept of preparing your breakfast while waiting for tea to be ready. As in running functions in parallel as opposed to back to back, given that there's enough processing power. Showing GUI is one thread, running the processes you want to display is another, so multi-threading. – Nae Jan 21 '18 at 16:54

0 Answers0