Bottom Page

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()

Quote
#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)
Quote
#3
Hi
thank u for the answer.

Have i change the labels in text widget?
Quote
#4
Not sure what you're asking
Quote
#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
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter GUI, problem running seperate files fishglue 17 467 Oct-15-2019, 02:56 PM
Last Post: Denni
  [python] [Tkinter] Problem bidding combobox with np.array NEL 3 212 Aug-04-2019, 11:07 AM
Last Post: Yoriz
  Problem with tkinter and string Dule 5 431 May-26-2019, 10:57 AM
Last Post: Yoriz
  [Tkinter] Problem with tkinter search widget poopcupine 1 501 Mar-25-2019, 08:24 AM
Last Post: Larz60+
  How to format a list when displaying in a tkinter Listbox nortski 3 3,271 Apr-03-2018, 02:31 AM
Last Post: Larz60+
  Tkinter Listbox tab char issues ashtona 4 1,394 Mar-27-2018, 12:28 PM
Last Post: ashtona
  Problem with tkinter sylas 23 18,826 Feb-17-2018, 11:57 PM
Last Post: sparkz_alot
  [Tkinter] problem with refresh UI in tkinter app Philbot 5 3,679 Feb-06-2018, 01:10 PM
Last Post: Philbot
  Problem with tkinter widget list Larz60+ 2 2,000 Nov-27-2017, 03:36 PM
Last Post: Larz60+
  [Tkinter] THow to search a string in a file and display it in listbox by Tkinter mosavit 2 2,325 Jun-21-2017, 08:02 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)