Python Forum
clear button destroy can't work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
clear button destroy can't work
#1
why my destroy can't work when click clear
from tkinter import *

a=Tk()
a.title('Window')
a.geometry('500x500+300+100')
b=StringVar() #this will store value of textbox, now we have to print it so
def com():
    c=b.get() #this will get the value in C
    lab12=Label(text=c, font=20, fg='green').pack() #it will now print value in c
	
def com1():

   #clear.destroy()
   #c=b.get()
   #text11.destroy()
   #text11.delete(0, 'end')
   destroy()

labl1=Label(text='Functionalityh to a button', font=30).pack() #label
button1=Button(text='Press to print', command=com).pack()
button2=Button(text='clear', command=com1).pack()
text11 = Entry(textvariable=b).pack()



a.mainloop()
Reply
#2
Star imports are bad.
You need also better names, a, b, com, com1 are meaningless names.
Better names are textbox_var or label_destroy.
What I did not know before, that you can let out the root and it still works, but it's bad.
Later if you work with Frames, you have to pass the right reference.

Here an example:

#/usr/env/bin python3
"""
Description of program
"""

from tkinter import (
    Tk, Button, Label,
    StringVar, Entry, END,
)
 

def label_create():
    """
    Description ...
    """
    text = textbox_var.get()
    # you need the reference of labels to destroy them later
    # using pack method on label, returns None
    label = Label(root, text=text, font=20, fg='green')
    label.pack()
    label_references.append(label)


def label_destroy():
    """
    Description ...
    """
    for label in label_references:
        label.destroy()
    label_references.clear()
    entry.delete(0, END)


label_references = []
root = Tk()
root.title('Window')
root.geometry('500x500+300+100')
textbox_var = StringVar()
Label(root, text='Functionalityh to a button', font=30).pack()
Button(root, text='Press to print', command=label_create).pack()
Button(root, text='Clear', command=label_destroy).pack()
Button(root, text='Exit', command=root.destroy).pack()
# reference for Entry
entry = Entry(root, textvariable=textbox_var)
entry.pack()
root.mainloop()
So everything is still on module level, imports have been changed and better names are given.
Later you want to isolate everything. But I guess you are not so far to use classes with inheritance.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,479 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
Exclamation [Tkinter] Error when closing the main window with destroy TomasSanchexx 1 727 Aug-06-2023, 01:54 AM
Last Post: deanhystad
  [Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed knoxvilles_joker 6 15,274 Apr-25-2021, 08:41 PM
Last Post: knoxvilles_joker
  Menu destroy Heyjoe 5 3,282 Mar-02-2021, 01:45 AM
Last Post: Heyjoe
  Button to clear all output labels? AnunnakiKungFu 5 2,767 Dec-18-2020, 10:12 AM
Last Post: backoboy10
  Reset Button did not work ardvci 2 2,720 Mar-02-2020, 07:59 PM
Last Post: ardvci
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,947 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Button click doesnt work from my second class/layout in Python imamideb 0 2,326 Feb-13-2018, 12:09 PM
Last Post: imamideb
  destroy function issue SmokerX 8 4,620 Jan-26-2018, 11:21 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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