Python Forum
[Tkinter] tkinter listbox problem
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] tkinter listbox problem
#1
Hi
i have a problem with the Listbox on Tkinter.
When i select with mouse one word in the Listbox, the program give me the relative code.
If i select another word again in the Listbox the program put the new code under the first one.
I would like that at the second selection the code before disappear.

What can I have to change?

THANK U!

(sorry for my english Smile )

This is my code:

from tkinter import *
from tkinter.font import Font

root = Tk()
text = Text(root)

myFont = Font(family="Times New Roman", size=12)
text.configure(font=myFont)



class articoli: 

    def __init__(self,codice,modello,pezzi_ora,cod_materiale,mis_materiale,
                 ultima_mod,materiale_casa):

        self.codice = codice
        self.modello = modello
        self.pezzi_ora = pezzi_ora
        self.cod_materiale = cod_materiale
        self.mis_materiale = mis_materiale
        self.ultima_mod = ultima_mod
        self.materiale_casa = materiale_casa

        

        self.label_8 = Label() 
        self.label_8.configure(text=self.codice,font=myFont)
        self.label_8.pack(side=TOP)

        self.label_9 = Label() 
        self.label_9.configure(text=self.modello,font=myFont)
        self.label_9.pack(side=TOP)

        self.label_10 = Label() 
        self.label_10.configure(text=self.pezzi_ora,font=myFont)
        self.label_10.pack(side=TOP)

        self.label_11 = Label() 
        self.label_11.configure(text=self.cod_materiale,font=myFont)
        self.label_11.pack(side=TOP)

        self.label_12 = Label() 
        self.label_12.configure(text=self.mis_materiale,font=myFont)
        self.label_12.pack(side=TOP)

        self.label_13 = Label() 
        self.label_13.configure(text=self.ultima_mod,font=myFont)
        self.label_13.pack(side=TOP)

        self.label_14 = Label() 
        self.label_14.configure(text=self.materiale_casa,font=myFont)
        self.label_14.pack(side=TOP)


def callback(event):
    value=str((Lb1.get(ACTIVE)))
    print (value)
    sys.stdout.flush()
    
    if value== "Python":
        Ps_48 = articoli("Ps 48","Patt_inf_est","1500","NG00002","135_5","20_04_2018",
                 "20")
    elif value== "Perl":
        Ps_40 = articoli("Ps_40","patt_sup_est","1000","NG00001",
                                 "130_4","18_05_2018","2000") 
    elif value== "C":
        Ps_42 = articoli("Ps_40","patt_sup_est","1000","NG00001",
                                 "135_4","18_05_2018","3000")
    elif value== "PHP":
        Ps_45 = articoli("Ps_40","patt_sup_est","1000","NG00001",
                                 "139_4","18_05_2018","4000")
    elif value== "JSP":  
        Ps_47 = articoli("Ps_40","patt_sup_est","1000","NG00001",
                                 "140_4","18_05_2018","5000")    
      

#listbox definition
Lb1 = Listbox(selectmode=SINGLE)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")

Lb1.pack(side=LEFT,fill=BOTH,expand=YES)
#scrollbar configuration       
scrollbar = Scrollbar()
scrollbar.pack(side=LEFT, fill=Y)
#listbox configuration   
Lb1.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=Lb1.yview)
#double click for listbox
Lb1.bind('<Double-Button-1>',callback)

root.mainloop()
Reply
#2
it looks like misuse of class.
each one of entries (like example below) instantiates a new copy of articoli that is separate from all others:
remember that a class is a structure, or template if you will, that 'creates' the class when called (instantiated)
    if value== "Python":
        Ps_48 = articoli("Ps 48","Patt_inf_est","1500","NG00002","135_5","20_04_2018",
                 "20")
You should instead use a text widget, instantiated once at the beginning of code
then a separate display method which as one of the first commands erases the text box using code like:
    textboxname.delete('1.0', END)
Reply
#3
Hi
thank u for the answer.

Have i change the labels in text widget?
Reply
#4
Not sure what you're asking
Reply
#5
Hi edoardo5782

One possible solution for your APP:
from functools import partial
import tkinter as tk

APP_TITLE = "Code Selector"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 300


class Application(tk.Frame):
    
    def __init__(self, master):
        self.master = master
        tk.Frame.__init__(self, master)

        self.languages = ["Python", "Perl", "C", "PHP", "JSP"]
        self.codes = [
            "Ps 48, Patt_inf_est, 1500, NG00002, 135_5, 20_04_2018, 20",
            "Ps_40, patt_sup_est, 1000, NG00001, 130_4, 18_05_2018, 2000",
            "Ps_42, patt_sup_est, 1000, NG00001, 135_4, 18_05_2018, 3000",
            "Ps_45, patt_sup_est, 1000, NG00001, 139_4, 18_05_2018, 4000",
            "Ps_47, patt_sup_est, 1000, NG00001, 140_4, 18_05_2018, 5000"
            ]
        self.num_code_items = len(self.codes[0].split(','))
        print("Code-Items:", self.num_code_items)
        self.code_dict = dict()
        self.label_vars = list()
            
        self.build()
        
    def build(self):
        listbox_frame = tk.LabelFrame(self, text="Languages:", bd=0,
            font='bold', fg='steelblue')
        listbox_frame.pack(side='left', fill='y', expand='yes', padx=5)
        
        self.listbox = tk.Listbox(listbox_frame, selectmode='single',)
        self.listbox.pack(side='left', fill='both', expand='yes')
        self.listbox.bind('<Double-Button-1>', self.output_code)
        
        label_frame = tk.LabelFrame(self, text="Code-Items:", bd=0,
            font='bold', fg='steelblue')
        label_frame.pack(side='left', fill='y', expand='yes', padx=5)

        for index, language in enumerate(self.languages):
            self.listbox.insert("end", language)
            self.code_dict[language] = self.codes[index]
        
        code_item_frame = tk.Frame(label_frame, relief='raised', bd=1)
        code_item_frame.pack(expand='yes')
            
        for code_item_index in range(self.num_code_items):
            label_var = tk.StringVar()
            tk.Label(code_item_frame, width=20, textvariable=label_var,
                anchor='w').pack()
            self.label_vars.append(label_var)
            
    def output_code(self, event):
        language = str((self.listbox.get('active')))
        print(language)
        
        code_data = self.code_dict[language].split(',')
        for item_index, code_item in enumerate(code_data):
            self.label_vars[item_index].set(code_item)
            
def main():
    app_win = tk.Tk()
    app_win.title(APP_TITLE)
    app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    #app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    
    app = Application(app_win)
    app.pack(fill='both', expand=True, padx=5, pady=5)
    
    app_win.mainloop()
 
 
if __name__ == '__main__':
    main()      
Greetings wuf
Reply
#6
Hi Wuf.
Thank u so much for your answer.
I think I m going to work hard to understand all your code :).
But that s why i posted my code.
Greetings Edoardo.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 tkinter radiobutton problem Nick_tkinter 14 5,828 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 3,482 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 2,161 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  tkinter| listbox.insert problem Maryan 3 3,437 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  What units does the width parameter of a tkinter Listbox use? Pedroski55 2 5,441 Jul-04-2020, 08:17 AM
Last Post: deanhystad
  Tkinter problem DPaul 6 4,040 May-28-2020, 03:40 PM
Last Post: DPaul
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 2,521 Apr-17-2020, 08:12 PM
Last Post: johnjh
  [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 4,530 Feb-27-2020, 05:17 PM
Last Post: Jan_97
  [Tkinter] Tkinter problem catlessness 1 2,008 Jan-15-2020, 05:17 AM
Last Post: Larz60+
  Problem with Submit button Tkinter Reldaing 2 3,602 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020