1

我正在使用 Python 3.8 和 Tkinter 进行聊天。我已经通过 Text 小部件的 window.create() 方法实现了彩色文本。但是当您开始向上滚动时,此文本会与程序界面的其余部分重叠。我怎样才能解决这个问题? 截屏

这是我正在使用的代码:

    def coloredOutput(self, bg, fg, text):
        try:
            a = []
            for line in text.split('\n'):
                a.append(len(line))

            if len(a) <= 10:
                label = Text(bg=bg, fg=fg, width=max(a), height=len(a), borderwidth=0)
                label.insert(1.0, text)
                label.configure(state=DISABLED)
            else:
                msg = "~ too many lines! 10 is max, but there's " + str(len(a)) + " lines in this message ~"
                label = Text(bg="red", fg="white", width=len(msg), height=1, borderwidth=0)
                label.insert(1.0, msg)
                label.configure(state=DISABLED)
        except Exception as e:
            print(e)
            msg = "Formatting error! Maybe your message is not properly formatted?"
            label = Text(bg="red", fg="white", width=len(msg), height=1, borderwidth=0)
            label.insert(1.0, msg)
            label.configure(state=DISABLED)
        self.text.window_create(END, window=label)

谢谢你的帮助。对不起,我的英语有任何错误,我来自乌克兰。

4

1 回答 1

2

添加的窗口必须是window_create文本小部件的子窗口,而不是根窗口的子窗口。

label = Text(self.text, ...)
self.text.window_create(END, label)
于 2020-08-25T13:33:50.277 回答