Search code examples
pythonpython-2.7gtkpygtk

PyGtk text buffer using to much memory want to clean it but then iter dont work


So I am just learning how to code and Im using pygtk to make a gui for a irc bot that im working on. The bot as some fun commands and stuf, but what im really here for is that I print the chat messages into a textbuffer and it makes memory go up, wich really isnt a problem unless you have a huge ammount of messages or want this to be on all the time. But with this problems in mind I wanted the bot to when he reached like 500 messages in the textbuffer(or 500 lines) he 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 dont 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...

HALP PLOX! xD

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