Python Forum
[Tkinter] Tkinter delete combobox content and not the list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter delete combobox content and not the list
#1
Good morning all,

I want to delete to zero my "combobox" for a next entry. I have read is studied a lot of tutorials, but I found nothing to get to just delete the selected content in the field. I'm missing something, but I can't see it. Could you please help me. I am progressing slowly, but surely Smile .

Traceback (most recent call last):
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\xxxxxx\Documents\PythonDevProgramme\MnesisProject\main4.py", line 358, in <lambda>
    command=lambda : save_citation(combo_auteur.get(), combo_theme.get(), entry_citation.get('1.0', END), entry_ref.get('1.0', END)))
  File "C:\Users\xxxxx\Documents\PythonDevProgramme\MnesisProject\main4.py", line 306, in save_citation
    combo_auteur.subwidget_list['cmb_nomsauteur'].delete(0, Tk.END)
AttributeError: 'Combobox' object has no attribute 'subwidget_list'

Here are my comboboxes:


def cmb_nomsauteur(event=None):
    connexion = sqlite3.connect('mnesis.db')
    cursor = connexion.cursor()
    cursor.execute('SELECT auteur_auteur FROM tb_auteur ORDER BY auteur_auteur')
    data = []
    for row in cursor.fetchall():
        data.append(row[0])
def cmb_theme(event=None):
    connexion = sqlite3.connect('mnesis.db')
    cursor = connexion.cursor()
    cursor.execute('SELECT theme_theme FROM tb_theme ORDER BY theme_theme')
    data = []
    for row in cursor.fetchall():
        data.append(row[0])
    return data
My function and lines 18 to 23 we find the subject of my request:

def save_citation(citation_auteur, citation_theme, citation_citation, citation_reference):
    """ Insertion des données dans database tb_citation """
    try:    
            connection = sqlite3.connect('mnesis.db') # Connection à la BdD
            cursor = connection.cursor() # création du curseur
            #création variable new_citation
            req = "INSERT INTO tb_citation( citation_auteur, citation_theme, citation_citation, citation_reference) VALUES ('"+citation_auteur+"','"+citation_theme+"', '"+citation_citation+"', '"+citation_reference+"')"
            cursor.execute(req)
 
    except Exception as e:
            print("ERREUR",e)
            connection.rollback()
    finally:
            cursor.close()
            connection.commit() # Valider l'enregistrement dans la database
            connection.close() ## Fermeture de la connection
 
            # Delete textbox after records
            combo_auteur.subwidget_list['cmb_nomsauteur'].delete(0, Tk.END)
            combo_theme.subwidget_list['cmb_theme'].delete(0, Tk.END)
            entry_citation.delete('1.0', END)
            entry_verset.delete('1.0', END)
            entry_ref.delete('1.0', END)
My form:

def write_citation():
    global combo_auteur
    global combo_theme
    global entry_citation
    global entry_verset
    global entry_ref
 
    tpl_citation = Toplevel()
    tpl_citation.title(" Bienvenue dans la saisie des citations")
    screen_x = int(tpl_citation.winfo_screenwidth())
    screen_y = int(tpl_citation.winfo_screenheight())
    tpl_citation_x = 1024
    tpl_citation_y = 600
    pos_x = (screen_x // 2) - (tpl_citation_x // 2)
    pos_y = (screen_y // 2) - (tpl_citation_y // 2)
    geo = "{}x{}+{}+{}".format(tpl_citation_x, tpl_citation_y, pos_x, pos_y)
    tpl_citation.geometry(geo)
    tpl_citation.resizable(width=False,height=False)
    tpl_citation.configure(bg='lightblue3')
    #---
    citation_auteur_label = Label(tpl_citation, text="Auteur",bg='lightblue3', font=("Arial", 12,"bold"))
    citation_auteur_label.place(x=30,y=38)
    combo_auteur = ttk.Combobox(tpl_citation, values=cmb_nomsauteur(), width=25, font=("Century Gothic", 14,"bold")) 
    combo_auteur.bind('<<ComboboxSelected>>', cmb_nomsauteur)
    combo_auteur.place(x=30,y=65)
    citation_theme_label = Label(tpl_citation, text="Thème",bg='lightblue3', font=("Arial", 12,"bold"))
    citation_theme_label.place(x=450,y=38)
 
    combo_theme = ttk.Combobox(tpl_citation, values=cmb_theme(), width=25, font=("Century Gothic", 14,"bold")) 
    combo_theme.bind('<<ComboboxSelected>>', cmb_theme)
    combo_theme.place(x=450,y=65)
    #------------       
    citation_label = Label(tpl_citation, text="Citation",bg='lightblue3', font=("Arial", 12,"bold"))
    citation_label.place(x=30,y=100)
    entry_citation = Text(tpl_citation, width="107", height="19", font=("Arial", 12))
    entry_citation.place(x=30,y=127)
    ref_label = Label(tpl_citation, text="Référence",bg='lightblue3', font=("Arial", 12,"bold"))
    ref_label.place(x=30,y=485)
    entry_ref = Text(tpl_citation, width="107", height="1",font=("Arial", 12))
    entry_ref.place(x=30,y=512)
    boutonEnregistrer = Button(tpl_citation, text='Enregistrer', 
                               command=lambda : save_citation(combo_auteur.get(), combo_theme.get(), entry_citation.get('1.0', END), entry_ref.get('1.0', END)))
    boutonEnregistrer.pack(side=BOTTOM, anchor=SE, padx=20, pady=20) 
 
    # end
    tpl_citation.mainloop()
Reply
#2
The document on ttk combobox docs
it inherits from entry, you wrote: combo_auteur.subwidget_list['cmb_nomsauteur'].delete(0, Tk.END)
to- combo_auteur.delete(0, Tk.END)
which will clear it.
Reply
#3
Thank you so much,
And if I understood correctly...
but i still miss some notions

            # Delete textbox after records
            combo_auteur.delete(0, Tk.END)
            combo_theme.delete(0, Tk.END)
            entry_citation.delete('1.0', END)
            entry_verset.delete('1.0', END)
            entry_ref.delete('1.0', END)
I still have an error message

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\xxxx\Documents\PythonDevProgramme\MnesisProject\main5.py", line 358, in <lambda>
    command=lambda : save_citation(combo_auteur.get(), combo_theme.get(), entry_citation.get('1.0', END), entry_ref.get('1.0', END)))
  File "C:\Users\xxxxx\Documents\PythonDevProgramme\MnesisProject\main5.py", line 306, in save_citation
    combo_auteur.delete(0, Tk.END)
AttributeError: type object 'Tk' has no attribute 'END'
my function :

def save_citation(citation_auteur, citation_theme, citation_citation, citation_reference):
    try:    
            connection = sqlite3.connect('mnesis.db') 
            cursor = connection.cursor() 
            #création variable new_citation
            req = "INSERT INTO tb_citation( citation_auteur, citation_theme, citation_citation, citation_reference) VALUES ('"+citation_auteur+"','"+citation_theme+"', '"+citation_citation+"', '"+citation_reference+"')"
            cursor.execute(req)
                                                  
    except Exception as e:
            print("ERREUR",e)
            connection.rollback()
    finally:
            cursor.close()
            connection.commit() 
            connection.close() 

            # Delete textbox after records
            combo_auteur.delete(0, Tk.END)
            combo_theme.delete(0, Tk.END)
            entry_citation.delete('1.0', END)
            entry_verset.delete('1.0', END)
            entry_ref.delete('1.0', END)
Reply
#4
try 'end' as a string combo_auteur.delete('0','end')
Reply
#5
Great, you're really cool. Wedge works very well, that's right

def clear():
    """ Delete textbox after records """
    
    combo_auteur.delete('0','end')
    combo_theme.delete('0','end')
    entry_citation.delete('1.0', END)
    entry_ref.delete('1.0', END)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,333 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 5,058 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  [Tkinter] Tkinter delete values in Entries, when I'm changing the Frame robertoCarlos 11 5,647 Jul-29-2020, 07:13 PM
Last Post: deanhystad
  [PyQt] Help: check content of combobox in horizontal header of QTableWidget mart79 1 3,267 Jul-26-2020, 06:18 PM
Last Post: deanhystad
  Convert combobox user input in to date with tkinter Ame 8 6,659 Jul-01-2020, 09:40 PM
Last Post: Yoriz
  Tkinter Generate/Delete Shapes with Keyboard Events baseball2201 3 3,486 Aug-16-2019, 12:23 PM
Last Post: Larz60+
  [python] [Tkinter] Problem bidding combobox with np.array NEL 3 3,346 Aug-04-2019, 11:07 AM
Last Post: Yoriz
  How to delete text from a tkinter Text widget? Tang 1 28,594 May-20-2018, 09:26 PM
Last Post: Larz60+
  comboBox to list files in a folder? ZipSnipe 3 5,889 Jan-23-2018, 07:04 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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