简体   繁体   English

完全模仿tkinter文本小部件中的stdout,以支持Python的打印功能中的end参数

[英]Fully mimic stdout in tkinter text widget to support the end parameter from Python's print function

How do I fully mimic sys.stdout in a tkinter text widget so that I can use the print() function (mainly the end parameter) to its full capacity? 如何在tkinter文本小部件中完全模仿sys.stdout ,以便我可以将print()函数(主要是end参数)用于其全部容量? So that I can use print("Hello World", end='\\r') to print to the text widget where if I print something else it will overwrite the previously printed text. 这样我就可以使用print("Hello World", end='\\r')打印到文本小部件,如果我打印其他内容,它将覆盖以前打印的文本。

class Main:
    def __init__(self):
        self.master = tk.Tk()
        self.output = tk.Text(self.master)
        self.output.pack()
        sys.stdout = self
        self.master.mainloop()

    def write(self, txt):
        self.output.insert(tk.INSERT, str(txt))
        self.output.see('end')

    def flush(self):
        # What do I do here?

Running 运行

print("Hello World", end='\r')
print("Goodbye World", end='\r')

Gives the following in the text widget 在文本小部件中提供以下内容

Hello World

Goodbye World

But I expect it to show the following instead 但我希望它能显示以下内容

Goodbye World

As a bonus question, how would I (re)define the flush functionality of stdout to work with a tkinter text widget? 作为一个额外的问题,我如何(重新)定义stdout的刷新功能以使用tkinter文本小部件?

I fixed the problem with the return carriage by updating the write function with the following: 我通过使用以下更新write函数修复了返回托架的问题:

def write(self, txt):
    if '\r' in txt:
        self.return_carriage = True
        return
    if self.return_carriage:
        self.output.delete(tk.INSERT + " linestart", tk.INSERT + " lineend")
        self.return_carriage = False
    self.output.insert(tk.INSERT, str(txt))
    self.output.see('end')

This only solves the problem when using end='\\r' in the print() function though. 这只能解决在print()函数中使用end='\\r'时的问题。 This still does not mimic the full sys.stdout.write functionality. 这仍然不能模仿完整的sys.stdout.write功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将标准输出重定向到 tkinter 文本小部件 - redirect stdout to tkinter text widget 如何将实时STDOUT从导入模块重定向到python中的Tkinter Text Widget? - How to redirect in real time STDOUT from imported module to Tkinter Text Widget in python? Python Pexpect-将子标准输出重定向到Tkinter文本小部件 - Python pexpect - redirect child stdout to a tkinter text widget 如何从窗口小部件的函数返回值,并将其传递给Tkinter,Python中的另一个窗口小部件的函数 - How to return value from a widget`s function, and pass it to another widget's function in Tkinter, Python 使用tkinter文本小部件作为另一个程序的标准输出 - Use a tkinter Text widget as stdout for another program 如何将标准输出重定向到 Tkinter 文本小部件 - How to redirect stdout to a Tkinter Text widget 使用线程将stdout重定向到Tkinter文本小部件的问题 - Issue with Redirecting stdout to Tkinter text widget with threads 如何在 Tkinter 文本小部件中打印字典的项目? - How to print Dictionary's items in Tkinter Text Widget? 从 function 更新 tkinter 中的文本小部件 - Update text widget in tkinter from function Python - 来自 ttk 中 tkinter 的文本小部件 - Python - Text widget from tkinter in ttk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM