Multiline textbox

def gui_init():
    global textbox_my_textbox

    #----- LIVE PROCESSING BOX -----
    MonospaceFont = font.Font(family="Courier", size=10)
    textbox_my_textbox = tk.Text(
        root,
        bg="white",
        font=MonospaceFont,
        wrap = "word",
        height=8,
        width=50
    )
    textbox_my_textbox.pack(padx=10, pady=10)
    textbox_my_textbox.place(x=10, y=310)
    textbox_my_textbox.insert(tk.END, "Loading...\n")

Overwrite all text

    NewText = "Hello\nNew content"
    textbox_my_textbox.delete("1.0", tk.END)
    textbox_my_textbox.insert("1.0", NewText)

Multiline textbox that auto removes top line once full example

#*******************************************************
#*******************************************************
#********** ADD A LINE TO LIVE PROCESSING BOX **********
#*******************************************************
#*******************************************************
def textbox_my_textbox_add_line (new_line):
    global textbox_my_textbox

    #Get current line count
    LineCount = int(textbox_my_textbox.index("end-1c").split('.')[0])

    #Delete top line if we're over # lines
    if LineCount >= 7:
        textbox_my_textbox.delete("1.0", "2.0")        #Delete the first line (from line 1, char 0 to line 2, char 0)

    #Add a timestamp to the start of the new line
    #time_stamp = datetime.now().strftime("%H:%M:%S")
    #new_line = f"{time_stamp} {new_line}"

    #Add the new line to the textbox
    textbox_my_textbox.insert(tk.END, new_line + "\n")

    gui_update()

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *