Jump to content

Object-Oriented Programming/GUI Applications/Text

From Wikiversity
"""This program demonstrates a tkinter text window.

Input:
    None

Output:
    Text window

References:
    https://www.pythontutorial.net/tkinter/tkinter-text/
    https://www.tutorialspoint.com/python/python_gui_programming.htm
    https://www.python-course.eu/python_tkinter.php
"""

import tkinter


class Root(tkinter.Tk):
    """Creates root window."""

    def __init__(self, *args, **kwargs):
        tkinter.Tk.__init__(self, *args, **kwargs)

        self.title("tkinter Text Example")
        self.geometry("%dx%d+0+0" % self.maxsize())

        text = TextFrame(self)
        text.pack(expand=True, fill=tkinter.BOTH)
        text.text = "Type in the textbox"


class TextFrame(tkinter.Frame):
    """Creates text window."""

    _text = None

    @property
    def text(self):
        return self._text.get(1.0, tkinter.END)

    @text.setter
    def text(self, value):
        self._text.delete(1.0, tkinter.END)
        self._text.insert(tkinter.END, str(value))

    def __init__(self, *args, **kwargs):
        tkinter.Frame.__init__(self, *args, **kwargs)

        self._text = tkinter.Text(self)
        self._text.pack(expand=True, fill=tkinter.BOTH)


if __name__ == "__main__":
    root = Root()
    root.mainloop()