0

Hi guys I'm making this chat app with tkinter and it works perfectly except by the fact that the text I post in my Listbox, don't shows up the way I want. Once the space in the Listbox finishes, I have to manually scroll down to see recent messages. My question is, what can I do to make the Listbox automatically scroll down when it gets fill. This is the part of the code of the Listbox lista=Listbox(raiz,font=('Arial')) lista.pack(side=LEFT,padx=10,pady=10,ipadx=200,fill=X,expand=True )

And this is where I posted:

def post(text,n=0):
   winsound.Beep(400,150)
   if n==0:
      lista.insert(END,text)
      campo.delete(0,1000)  
   else:
      for i in text:
          lista.insert(END,i)

If you can help me it will be great. Thanks!!!

5
  • i think the you should clear the Listbox and display the current one. Commented Feb 19, 2018 at 15:46
  • The listbox has a see method to scroll a particular item into view. Commented Feb 19, 2018 at 19:47
  • Please rename your title that emphasizes the specific issue you're having. Right now the title leads to nowhere. Commented Feb 19, 2018 at 21:09
  • @Bryan Oakley you should put your comment in an answer to mark it as correct because that was the method i used, and it works perfectly Commented Mar 21, 2018 at 19:51
  • You don't need to use Listbox @amh9412 Commented Mar 28, 2019 at 15:59

2 Answers 2

3

You need add this line lista.see(tkinter.END) before lista.insert(END,text)

Sign up to request clarification or add additional context in comments.

Comments

1

You should clear the Listbox widget before you display the current or insert

def post(text,n=0):
   winsound.Beep(400,150)
   if n==0:
      lista.delete(0, END) # this to clear the listbox and display the current content in the listbox
      lista.insert(END,text)
      campo.delete(0,1000)  
   else:
      for i in text:
          lista.insert(END,i)

Or you can place the new content at top of Listbox like this,

def post(text,n=0):
   winsound.Beep(400,150)
   if n==0:
      return
      lista.insert(n-1, text)
      campo.delete(0,1000)  
   else:
      for i in text:
          lista.insert(END,i)

Since you didn't post your full code or minimal am assuming this how it is.

4 Comments

This way I can't see a history of received messages or will I?
A comment, n, is just a marker if its value is 0 it means that the action triggering the function is a Button or a press key defined with bind(), if it is 1 it means that the action triggering the function is an incoming socket messages
i don't know what code you have at your end, create minimal code so you can get help.With this you are giving me burden .
I find a way to automatically scroll down the Listbox always a text is posted. The way I do it is with lista.see(END), this way always a text is inserted with lista.insert(END,text), the listbox automatically will show the END that is where the new text is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.