Search code examples
python-3.xtkinterpython-3.8tkinter-text

Tkinter window_create in text widget overlaps other GUI


I'm making a chat using Python 3.8 and Tkinter. I have implemented colored text via window.create() method of Text widget. But when you start scrolling up, this text overlaps the rest of the program's interface. How can I fix this? screenshot

Here's code that i'm using:

    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)

Thanks for your help. Sorry for any mistakes in my english, I'm from Ukraine.


Solution

  • The window that is being added with window_create needs to be a child of the text widget rather than a child of the root window.

    label = Text(self.text, ...)
    self.text.window_create(END, label)