Message Text Module

import tkinter
import tkinter.scrolledtext

from raconverter.constants import *


class MessageText:
    """
    Create and update a text entry.
    """

    @staticmethod
    def create_text_entry(widget):
        """
        Create a text entry with word wrap and a scrollbar.

        :param widget: The widget
        :return: A text entry object
        """
        text_entry = tkinter.scrolledtext.ScrolledText(widget)
        text_entry.config(width=TEXT_ENTRY_WIDTH46, height=TEXT_ENTRY_HEIGHT16, highlightbackground=BLACK, wrap=WRAP)
        text_entry.config(font=FONT, state=STATE_DISABLE)
        text_entry.pack(side=SIDE_TOP, fill=FILL_X, padx=TEXT_ENTRY_PADX10, expand=EXPAND_YES)

        return text_entry

    def insert_message(self, text_entry, entry_text, delete=1, index=1):
        """
        Insert text into the text entry at the given index.

        :param text_entry: A text entry object
        :param entry_text: String content
        :param delete: Do not delete existing entry text (0) | Delete existing entry text (1)
        :param index: Start of the string (0) | End of the string (1)
        """
        # Delete existing entry text if requested
        if delete == 1:
            self.delete_message(text_entry)

        # Enable write mode
        text_entry.config(state=STATE_ENABLE)

        # Insert entry text
        if index == 0:
            text_entry.insert(INDEX_START1, entry_text)
        elif index == 1:
            text_entry.insert(INDEX_END, entry_text)

        # Disable write mode
        text_entry.config(state=STATE_DISABLE)

    @staticmethod
    def delete_message(text_entry):
        """
        Delete all text content in a text entry.

        :param text_entry: A text entry object
        """
        # Enable write mode
        text_entry.config(state=STATE_ENABLE)
        text_entry.delete(INDEX_START1, INDEX_END)
        # Disable write mode
        text_entry.config(state=STATE_DISABLE)

Leave a Reply

Your email address will not be published.