1
我想用Python文本框将输出从Python shell重定向到Tkinter GUI。用于在Python中输出的文本框(基于文本的游戏)
例如,在我的游戏中点击库存将在Python shell中显示您的耗材。我希望它在GUI文本框中显示。
我该如何去做这件事?
我想用Python文本框将输出从Python shell重定向到Tkinter GUI。用于在Python中输出的文本框(基于文本的游戏)
例如,在我的游戏中点击库存将在Python shell中显示您的耗材。我希望它在GUI文本框中显示。
我该如何去做这件事?
您可以为您的文本处理程序使得:
class textbox_handler:
def __init__(self, text):
self.data = []
self.text = text #text is your tk text widget.
def write(self, s):
self.data.append(s)
def print_out(self):
for line in data:
self.text.insert('end',line)
然后,在你的主要功能:
import sys
def main():
handler = textbox_handler(text1)
sys.stdout = handler
print "sample text."
print "sample text #2."
print "another sample text."
#or you can use without setting sys.stdout
print >>handler, "yet another sample text."
每个print语句调用写处理程序对象的方法。你可以简单地从handler.data中检索这些字符串。
显示一些代码。你是否正在谈论将*从*或*重新定向到shell中并不清楚 – joaquin