How to limit the number of lines in a Listbox in Tkinter

Asked

Viewed 140 times

0

I’m studying about Tkinter and I came across a problem when I wanted to limit the amount of results that appeared on a Listbox. I looked it up online, but I couldn’t find anything on it.

I am developing a "Chat", where what I need is that only 10 last lines (messages) appear in Listbox, whose size will be delimited to support the 10 lines without the bearing. Results outside this limitation shall be hidden.

I’m using Python 3.8.1.

Thanks in advance.

Follow full code below:

from tkinter import *

class App():
    def __init__ (self, master = None):

        self.fonte_padrao = ("Arial", "12")

        self.frame = Frame(master).pack()        

        self.entry_nome = Entry(master, font = self.fonte_padrao)
        self.entry_nome.place(x = 0, y = 0, width = 300, height = 30)
        self.nome = None

        self.btn_cadastro = Button(master, text = "Entrar", command = self.entrar)
        self.btn_cadastro.place(x = 310, y = 0, width = 90, height = 30)

        self.lista_msg = Listbox(master, font = self.fonte_padrao)
        self.lista_msg.place(x = 10, y = 50, width = 380, height = 190)

        self.entry_txt = Text(master)
        self.entry_txt.bind("<Return>", self.btn_focus)
        self.entry_txt.place(x = 10, y = 250, width = 380, height = 65)

        self.btn = Button(master, text = "Enviar")
        self.btn.bind("<Return>", self.enviar_msg)
        self.btn.bind("<Button-1>", self.enviar_msg)
        self.btn.place(x = 175, y = 320, width = 50)

    def entrar(self):
        self.nome = self.entry_nome.get()
        self.entry_nome.destroy()
        self.btn_cadastro.destroy()
        self.lbl_nome = Label(self.frame, text = self.nome, font = self.fonte_padrao)
        self.lbl_nome.place(x = 0, y = 0, width = 400)

    def btn_focus(self, event):
        self.btn.focus()

    def enviar_msg(self, event):
        txt = self.entry_txt.get('0.0', END)        
        msg = self.nome + " - " + txt

        self.entry_txt.delete('0.0', END)
        self.entry_txt.focus()

        self.lista_msg.insert(END, msg)

root = Tk()
root.geometry("400x350")
root.title("Chat")
App(root)
root.mainloop()

1 answer

0


Face if you need that whenever the user type something and the listbox accompanies the last item you can use the see method of listbox

def enviar_msg(self, event):
    txt = self.entry_txt.get('0.0', END)        
    msg = self.nome + " - " + txt

    self.entry_txt.delete('0.0', END)
    self.entry_txt.focus()

    self.lista_msg.insert(END, msg)
    # O método see faz com que o listbox mostre o index desejado
    # passar o size da lista faz com que ele mostre o último item
    self.lista_msg.see(self.lista_msg.size())

but you can still see the conversation using the mouse roll if you only wanted to show the 10 messages and prevent the user from seeing the others, one option is to delete the first item from the list and add a new or disable the mouse roll Event Trigger

to make the option to remove the first item is quite simple

    # verifica se a lista possui 10 itens
    if self.lista_msg.size() >= 10:
        # caso tiver, remove o primeiro item da lista
        self.lista_msg.delete(0)
    self.lista_msg.insert(END, msg)

it is very difficult to find Tkinter material, I recommend the site elfbot or tutorialspoint

https://www.tutorialspoint.com/python/python_gui_programming.htm

  • Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.