我的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()
目前没有回答
相关问题 更多 >
编程相关推荐