Multiline textbox that auto removes top line once full example

def gui_init():
    global textbox_live_processing

    #----- LIVE PROCESSING BOX -----
    MonospaceFont = font.Font(family="Courier", size=10)
    textbox_live_processing = tk.Text(
        root,
        bg="white",
        font=MonospaceFont,
        height=8,
        width=50
    )
    textbox_live_processing.pack(padx=10, pady=10)
    textbox_live_processing.place(x=10, y=310)
    textbox_live_processing_add_line("Starting...")



#*******************************************************
#*******************************************************
#********** ADD A LINE TO LIVE PROCESSING BOX **********
#*******************************************************
#*******************************************************
def textbox_live_processing_add_line (new_line):
    global textbox_live_processing

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

    #Delete top line if we're over # lines
    if LineCount >= 7:
        textbox_live_processing.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_live_processing.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 mini sites like this. We hope you find the site 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 on this site. 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 *