1

My display class should run inputs as python code. But I don't know how to add exec code to my Text. For eaxmple if I type print("Hello WORLD")and press button in shell is output Hello WORLD but in Text not. Any ideas how I can get output to my Text output?

from tkinter import *
import sys
import code

class Display:

    def __init__(self):

        #window
        self.frame = Tk()
        self.entry = Entry(self.frame)
        self.entry.pack()
        self.button = Button(self.frame,text="DoIt", command=self.vypis)
        self.button.pack()
        self.output = Text(self.frame)
        self.output.pack()

        #console
        self.konzola = code.InteractiveInterpreter()


    def vypis(self):
        compiledText = self.kompiluj(self.entry.get())
        self.output.insert(END,str(exec(self.entry.get())))

    def kompiluj(self,code):
        return self.konzola.compile(code,'','exec')

    def loop(self):
        self.frame.mainloop()

if __name__ == '__main__':
    Display().loop()
dsv_dsv
  • 59
  • 3
  • 8
  • Try redirecting `sys.stdout` to a file-like object, which you can then read and insert into your output widget. – Kevin Jan 21 '15 at 13:14
  • For a more direct way of capturing the `stdout`, see [this answer](http://stackoverflow.com/a/16571630/3714930) – fhdrsdg Jan 21 '15 at 13:28

0 Answers0