Jac*_*b D 6 python pygame stdout
我在这里有一个非常奇怪的用例,我正在尝试为我的学生组合一些简单的程序来帮助他们学习 Python。为了让它工作,我在 TKinter 框架中嵌入了一个 PyGame 窗口,我需要重定向标准输出以更改 PyGame 窗口中的某些内容。我有重定向工作,如果我将它重定向到一个文件它工作正常,但如果我尝试更改文本它不起作用。我将一个字符串硬编码到 PyGame 文本更改代码中并且有效,但由于某种原因它不适用于重定向的文本。
重定向类:
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
Run Code Online (Sandbox Code Playgroud)
游戏类:
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()
Run Code Online (Sandbox Code Playgroud)
我像这样设置标准输出:
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()
Run Code Online (Sandbox Code Playgroud)
当我按原样运行它时,如果我在框中键入 hello , hello 将打印到文件中,但在 pygame 框中放入一个空字符。我真的不太了解 PyGame,无法知道这是那端的问题还是重定向问题。任何帮助将不胜感激!
(如果你想知道这里的用例是什么,我会让他们“完成”一些程序来在 PyGame 中发生一些事情。所以如果他们在给定的字段中输入 print('Hello Friend!')会将其重定向为 PyGame 框中某人的对话框。从长远来看,它可能不起作用,但我必须克服这个问题才能真正弄清楚)
编辑:
所以问题是,当我单击按钮时,由于某种原因,write 函数被调用了两次,它在键入的字符串上调用 print,然后在空字符串上再次调用。仍然不确定如何修复它,但至少我发现了问题
好的,所以我发现了问题。
看起来 print 向 stdout 发送了两件事:要打印的字符串和结束字符。所以它覆盖了我想要用换行符打印的字符串。我更改了 PrintTest 类以适应这种情况:
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
Run Code Online (Sandbox Code Playgroud)