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()
Thank you so much for your help!
– Nathan Bahia