[Tkinter] tkinter listbox problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] tkinter listbox problem (/thread-12604.html) |
tkinter listbox problem - edoardo5782 - Sep-02-2018 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 ) 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() RE: tkinter listbox problem - Larz60+ - Sep-02-2018 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) RE: tkinter listbox problem - edoardo5782 - Sep-04-2018 Hi thank u for the answer. Have i change the labels in text widget? RE: tkinter listbox problem - Larz60+ - Sep-04-2018 Not sure what you're asking RE: tkinter listbox problem - wuf - Sep-06-2018 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 RE: tkinter listbox problem - edoardo5782 - Sep-10-2018 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. |