Python运行代码特克斯

2024-04-20 10:22:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我的display类应该以python代码的形式运行输入。但我不知道如何在文本中添加exec代码。对于eaxmple,如果我键入print("Hello WORLD")并按下shell中的按钮,则输出Hello WORLD,而不是文本。有没有办法把输出转换成文本输出?你知道吗

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()

Tags: 代码文本importselfhelloworldoutputdef