pythonpython-2.7gtkpygtk

PyGtk text buffer using to much memory want to clean it but then iter doesn't work


I am just learning how to code and I'm using pygtk to make a GUI for an IRC bot that I'm working on. The bot has some fun commands and stuff, but what I'm really here for is that I print the chat messages into a textbuffer and it makes memory go up, which really isn't a problem unless you have a huge amount of messages or want this to be on all the time. But with this problems in mind I wanted the bot to when it reached like 500 messages in the textbuffer(or 500 lines) it would delete the first 250 from the text buffer so that it would keep the memory usage down and still plenty of messages to read to whoever was using the GUI.

My problem is that I don't really know how to do this, if I use iters they will become useless when delete the 250 lines because I've changed the textbuffer, and I also tried to use marks but it does look like I know how to use them...

Code can be found here. The textbuffer is located inside run.py and is named chatbuffer.


Solution

  • Solved it Kappa

    I use this for deletion:

    if chatbuffer.get_line_count() > 10:
        chatbuffer.delete(chatbuffer.get_iter_at_line(0), chatbuffer.get_iter_at_line(251))
    

    And instead of defining inters like:

    end_inter = chatbuffer.get_end_inter()
    

    I just straigth up use:

    chatbuffer.get_end_inter()
    

    Like this:

    chatbuffer.insert(chatbuffer.get_end_iter(), user + " > " + message + "\n")
    

    EDIT: WELL IM DUMB I created a whole another questin because of an error that I encouuntered after this because I tested this with 10 lines and deleting 5 and forgot to change it to 500 on the final code. gtk-error **: gtk_text_btree_previous_line ran out of lines aborting