0

我有这个文本框:

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
4

1 回答 1

2

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

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)
于 2020-10-12T10:39:00.877 回答