[英]Redirecting stdout in Python into PyGame
我这里有一个非常奇怪的用例,我正在尝试为我的学生编写一些简单的程序来帮助他们学习 python。为了让它工作,我有一个 PyGame window 嵌入在一个 TKinter 框架中,我需要重定向 stdout 以更改 PyGame window 中的某些内容。我有重定向工作,如果我将它重定向到一个文件它工作正常,但如果我尝试更改文本它不起作用。 我将一个字符串硬编码到 PyGame 文本更改代码中并且可以正常工作,但由于某些原因它不适用于重定向的文本。
重定向 class:
class PrintTest:
def __init__(self, file, game):
self.f = file
self.game = game
def write(self, t):
f.write(t)
self.game.text = game.font.render(t, True, self.game.text_colors[1], self.game.text_colors[2])
self.game.textRect = self.game.text.get_rect()
self.game.textRect.center = (300, 300)
def flush(self):
pass
游戏 class:
class Game:
def __init__(self, root):
self.root = root
embed = tk.Frame(root, width=600, height=600)
embed.pack(side=tk.LEFT)
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
if platform.system == "Windows":
os.environ['SDL_VIDEODRIVER'] = 'windib'
self.text_colors = [(255,255,255),
(0,255,0),
(0,0,128)]
# initialize a pygame display
pygame.init()
self.screen = pygame.display.set_mode((600, 600))
self.screen.fill(pygame.Color('red'))
self.clock = pygame.time.Clock()
self.font = pygame.font.Font('freesansbold.ttf', 32)
self.text = self.font.render('Hello, world!', True, self.text_colors[1], self.text_colors[2])
self.textRect = self.text.get_rect()
self.textRect.center = (300, 300)
# TK Creation
helv = font.Font(family='Helvetica', size=18, weight='bold')
self.button = tk.Button(root,
text="Change text",
font=helv,
command=self.change_text).pack()
self.user_input = tk.StringVar()
self.textbox = ttk.Entry(root, width=15, textvariable=self.user_input)
self.textbox.pack()
# ---------------------------------------------------------------
def change_text(self):
print(self.user_input.get())
def run(self):
# Pygame loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
self.screen.fill(pygame.Color('red'))
self.screen.blit(self.text, self.textRect)
pygame.display.update()
try:
self.root.update()
except tk.TclError:
running = False
pygame.quit()
我这样设置标准输出:
try:
root = tk.Tk()
root.geometry('1000x600')
game = Game(root)
f = open('text.txt', 'w')
sl = PrintTest(f, game)
sys.stdout = sl
game.run()
except Exception:
traceback.print_exc()
pygame.quit()
当我按原样运行时,如果我在框中键入 hello,则会将 hello 打印到文件中,但 null 字符会放入 pygame 框中。 我真的不太了解 PyGame,无法知道这是该端的问题还是重定向问题。 任何帮助将不胜感激!
(如果您想知道这里的用例是什么,我将让他们“完成”一些程序以在 PyGame 中发生某些事情。因此,如果他们在给定字段中键入 print('Hello Friend,')。它会将其重定向为 PyGame 框中某人的对话框,从长远来看可能无法正常工作,但我必须克服这一点才能真正弄清楚)
编辑:
所以问题是,当我单击按钮时,出于某种原因,write function 被调用了两次,它在输入的字符串上调用 print,然后在空字符串上再次调用。 仍然不确定如何解决它,但至少我找到了问题
好的,所以我发现了问题。
看起来 print 向 stdout 发送了两件事,即要打印的字符串和结束字符。 所以它覆盖了我想要用换行符打印的字符串。 我改变了我的 PrintTest class 来适应这个:
class PrintTest:
def __init__(self, file, game):
self.f = file
self.game = game
def write(self, t):
if t != '\n':
self.f.write(t)
self.game.text, self.game.textRect = game.font.render(t, self.game.text_colors[2])
self.game.textRect.center = (300, 300)
def flush(self):
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.