【问题标题】:python tkinter textbox console output to be always in last line?python tkinter 文本框控制台输出总是在最后一行?
【发布时间】:2020-10-12 10:19:16
【问题描述】:

我有这个文本框:

t_outputbox = tk.Text(tab_console, width=99, height=18, font='Tahoma 12',bg='#051da3', fg='#d9d9d9', relief="flat", highlightthickness=1, borderwidth=1)

并使用一个函数将所有控制台输出打印到它:

def redirector(inputStr): # send all prints to GUI instead of console
t_outputbox.config(state=NORMAL)
t_outputbox.insert(INSERT, inputStr)
t_outputbox.see(END)
t_outputbox.config(state=DISABLED)

并在我的代码的 ned 处使用这个命令来激活 stdout 以使用 redirector() 来编写。

但有时,当我单击控制台并上下滚动时,它会打印在中间的某个地方,而不是文本框的末尾。

是否有解决方案,以使新的打印件总是在盒子的末端敲打?

sys.stdout.write=redirector

【问题讨论】:

    标签: python tkinter console textbox stdout


    【解决方案1】:

    这是因为您在INSERT 的位置之后插入文本。这里,INSERT 表示光标的位置。如果要在文本小部件的末尾插入文本,则需要将 INSERT 替换为 END

    def redirector(inputStr): # send all prints to GUI instead of console
        t_outputbox.config(state=NORMAL)
        t_outputbox.insert(END, inputStr)
        t_outputbox.see(END)
        t_outputbox.config(state=DISABLED)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2022-12-12
      • 2022-01-10
      • 1970-01-01
      相关资源
      最近更新 更多