Aiuto python tkinter

DaniAffCH

Nuovo Utente
3
0
Ciao a tutti, sto facendo un programma in tkinter ma ho un problema: in origine c'è una entry e tramite un bottone si possono aggiungere altre entry. Voglio che quando si clicchi il bottone "cancella tutto" si cancellino le entry aggiunte e la pagina torni come in origine

Python:
class Add(tk.Frame):
    posy = 0.3063
    nentry = 1
    entry = list()
    var = list()
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(bg=color["giallo_bg"])
        l1 = tk.Label(self, text=title, font=('Helvetica', 60), bg=color["giallo_bg"])
        l2 = tk.Label(self, text="Aggiungi match", font=('Helvetica', 40), bg=color["giallo_bg"])
        bt_return = tk.Button(self, text='Ritorna alla home', font=('Helvetica', 15), command=lambda:controller.show_frame("Main"))
        l3 = tk.Label(self, text="Inserisci l'ingrediente principale", font=('Helvetica', 20), bg=color["giallo_bg"])
        l4 = tk.Label(self, text="Inserisci gli ingredienti secondari", font=('Helvetica', 20), bg=color["giallo_bg"])
        global var_main
        var_main = tk.StringVar(value='')
        global e1
        e1 = tk.Entry(self, textvariable=var_main, width=30)
        bt_more =  tk.Button(self, text='+ Match', font=('Helvetica', 15), command=self.more)
        self.var.append(tk.StringVar(value=''))
        self.entry.append(tk.Entry(self, textvariable=self.var[0], width=30))
        bt_save = tk.Button(self, text='Salva', font=('Helvetica', 15), command=self.save)
        bt_delete = tk.Button(self, text='Cancella tutto', font=('Helvetica', 15), command=self.delete)
        l1.pack()
        l2.pack()
        l3.place(relx=0, rely=0.2)
        l4.place(relx=0, rely=0.3)
        self.entry[0].place(relx=0.21, rely=self.posy)
        e1.place(relx=0.2, rely=0.2063)
        bt_more.place(relx=0.35, rely=0.3)
        bt_save.place(relx=0.35, rely=0.35)
        bt_return.place(relx=0.465, rely=0.9)
        bt_delete.place(relx=0.35, rely=0.4)

    def delete(self):
        for obj in self.entry:
            obj.delete(0, tk.END)
        e1.delete(0, tk.END)
        for obj in self.entry[1:]:
            obj.destroy()
            
    def more(self):
        if self.nentry == 19:
            pass
        else:
            self.posy += 0.03
            self.var.append(tk.StringVar(value=''))
            self.entry.append(tk.Entry(self, textvariable=self.var[len(self.entry)], width=30))
            self.entry[self.nentry].place(relx=0.21, rely=self.posy)
            self.nentry += 1
In questo modo posso cliccare cancella una sola volta, perchè se lo faccio nuovamente si bugga e succede questo: Schermata del 2018-08-20 20-53-35.png
Vi prego aiutatemi. Grazie mille!
 
Ultima modifica:

enricovela

Utente Attivo
443
124
CPU
Ryzen 3600
Dissipatore
Thermaltake Water 3.0 240
Scheda Madre
MSI Gaming plus max x470
HDD
970 evo plus;Crucial MX500 1TB; GIGABYTE M.2 PCIe SSD; p300
RAM
16 GB @3000 MHz
GPU
quadro fx 3800
Audio
Audioengine D1
Monitor
LG 32QK500
PSU
Straight power 11 650 W
Case
Cooler Master MB510L
Net
FTTH Tim
OS
Pop!_OS
Posta il codice completo
 

DaniAffCH

Nuovo Utente
3
0
Posta il codice completo
Python:
from function import *
try:
    import Tkinter as tk
except:
    import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        global color, title, w, h
        tk.Tk.__init__(self)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        color = {"giallo_bg": "#F7E89F"}
        title = "The perfect match"
        w = self.winfo_screenwidth()
        h = self.winfo_screenheight()
        self.title(title)
        self.geometry("%dx%d+0+0" % (w, h))
        self.resizable(False, False)
        self.overrideredirect(False)
        self.frames = {}
        photo = tk.PhotoImage(file = "icon.png")
        self.tk.call('wm', 'iconphoto', self._w, photo)
        for f in (Main, Add):
            page_name = f.__name__
            frame = f(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame("Main")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class Main(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(bg=color["giallo_bg"])
        l1 = tk.Label(self, text=title, font=('Helvetica', 60), bg=color["giallo_bg"])
        global var1
        var1 = tk.StringVar(value='')
        e1 = tk.Entry(self, textvariable=var1, width=60)
        bt_send = tk.Button(self, text='Invio', font=('Helvetica', 15), command=self.get)
        bt_exit = tk.Button(self, text='Esci', font=('Helvetica', 15), command=exit)
        bt_change = tk.Button(self, text='Aggiungi match', font=('Helvetica', 15), command=lambda: controller.show_frame("Add"))
        bt_change.pack()
        l1.pack()
        e1.place(relx=0.37, rely=0.15)
        bt_send.place(relx=0.63, rely=0.14)
        bt_change.place(relx=0.475, rely=0.82)
        bt_exit.place(relx=0.50, rely=0.90)

    def get(self):
        search = var1.get()
        print(database.read(search))

class Add(tk.Frame):
    posy = 0.3063
    nentry = 1
    entry = list()
    var = list()
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(bg=color["giallo_bg"])
        l1 = tk.Label(self, text=title, font=('Helvetica', 60), bg=color["giallo_bg"])
        l2 = tk.Label(self, text="Aggiungi match", font=('Helvetica', 40), bg=color["giallo_bg"])
        bt_return = tk.Button(self, text='Ritorna alla home', font=('Helvetica', 15), command=lambda:controller.show_frame("Main"))
        l3 = tk.Label(self, text="Inserisci l'ingrediente principale", font=('Helvetica', 20), bg=color["giallo_bg"])
        l4 = tk.Label(self, text="Inserisci gli ingredienti secondari", font=('Helvetica', 20), bg=color["giallo_bg"])
        global var_main
        var_main = tk.StringVar(value='')
        global e1
        e1 = tk.Entry(self, textvariable=var_main, width=30)
        bt_more =  tk.Button(self, text='+ Match', font=('Helvetica', 15), command=self.more)
        self.var.append(tk.StringVar(value=''))
        self.entry.append(tk.Entry(self, textvariable=self.var[0], width=30))
        bt_save = tk.Button(self, text='Salva', font=('Helvetica', 15), command=self.save)
        bt_delete = tk.Button(self, text='Cancella tutto', font=('Helvetica', 15), command=self.delete)
        l1.pack()
        l2.pack()
        l3.place(relx=0, rely=0.2)
        l4.place(relx=0, rely=0.3)
        self.entry[0].place(relx=0.21, rely=self.posy)
        e1.place(relx=0.2, rely=0.2063)
        bt_more.place(relx=0.35, rely=0.3)
        bt_save.place(relx=0.35, rely=0.35)
        bt_return.place(relx=0.465, rely=0.9)
        bt_delete.place(relx=0.35, rely=0.4)

    def delete(self):
        for obj in self.entry:
            obj.delete(0, tk.END)
        e1.delete(0, tk.END)
        for obj in self.entry[1:]:
            obj.destroy()

    def save(self):
        content = list()
        for count in range(len(self.entry)):
            content.append(self.var[count].get())
        main = var_main.get()
        database.save(main, content)
        self.delete()

    def more(self):
        if self.nentry == 19:
            pass
        else:
            self.posy += 0.03
            self.var.append(tk.StringVar(value=''))
            self.entry.append(tk.Entry(self, textvariable=self.var[len(self.entry)], width=30))
            self.entry[self.nentry].place(relx=0.21, rely=self.posy)
            self.nentry += 1

def process():
    if __name__ == '__main__':
        database.start()
        app = App()
        app.mainloop()
        database.stop()
process()
 

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!