Hello this is my first question on this platform. I got a problem with tkinter, I have an entry where the user can write a number. If the user writes something else and press save a label appears with "only floats allowed" so far so good. But if the user writes after the label a appears a number an presses yes, the label should be deleted. How can I delete the label after it plots the first time by correcting the entry and pressing save again? P.s. its my first try to Code a GUI so I'm thankful for other tipps & tricks.
import tkinter as tk
parison_window = tk.Tk()
parison_window.title("Create Parison")
parison_window.geometry("1000x1000")
pwt1_lbl = tk.Label(parison_window, text="PWT1")
pwt1_lbl.pack()
pwt1_lbl.place(x=30, y=130)
label = tk.Label(parison_window, text="1")
label.pack()
label.place(x=10, y=140 20 )
entry = tk.Entry(parison_window, width=5, justify="center")
entry.pack()
entry.place(x=30, y=140 20)
def check_and_save():
if entry.get():
try:
pwt1 = float(entry.get())
except ValueError:
error_text = tk.Label(parison_window, text="only floats allowed")
error_text.pack()
error_text.place(x=150, y=140 20 )
save_button = tk.Button(parison_window, text="save", command=check_and_save)
save_button.pack()
parison_window.mainloop()
CodePudding user response:
If you want to remove text from existing label then you can use error_text.config(text="")
or error_text["text"] = ""
.
If you want to remove all widget then you may do error_text.destroy()
But all this make problem because widget may not exists in some moments and trying to set text or destroy it may generate error.
You should rather create empty label at start and later only replace text in this label.
import tkinter as tk
# --- functions --- # PEP8: all functions before main code
def check_and_save():
if entry.get():
try:
pwt1 = float(entry.get())
error_text['text'] = ""
except ValueError:
error_text['text'] = "only floats allowed"
# --- main ---
parison_window = tk.Tk()
parison_window.title("Create Parison")
parison_window.geometry("1000x1000")
pwt1_lbl = tk.Label(parison_window, text="PWT1")
pwt1_lbl.place(x=30, y=130)
# ---
label = tk.Label(parison_window, text="1")
label.place(x=10, y=140 20)
entry = tk.Entry(parison_window, width=5, justify="center")
entry.place(x=30, y=140 20)
error_text = tk.Label(parison_window) # create label with empty text
error_text.place(x=150, y=140 20)
# ---
save_button = tk.Button(parison_window, text="save", command=check_and_save)
save_button.pack()
parison_window.mainloop()
PEP 8 -- Style Guide for Python Code
BTW:
pack()
and place()
(and grid()
) are different layout managers
and when you use place()
then you don't need pack()
(and grid()
)
EDIT:
Using destroy()
it would need to use global variable error_text
with default value None
at start. And later it would need to check if error_text
is not None
and destroy it (and assign again None
)
import tkinter as tk
# --- functions --- # PEP8: all functions before main code
def check_and_save():
global error_text # inform function to assign new value to global variable `error_text` instead of creating local variable `error_text`
if entry.get():
try:
pwt1 = float(entry.get())
if error_text is not None:
error_text.destroy()
error_text = None
except ValueError:
if error_text is not None:
error_text.destroy()
error_text = None
error_text = tk.Label(parison_window, text="only floats allowed")
error_text.place(x=150, y=140 20)
# --- main ---
parison_window = tk.Tk()
parison_window.title("Create Parison")
parison_window.geometry("1000x1000")
pwt1_lbl = tk.Label(parison_window, text="PWT1")
pwt1_lbl.place(x=30, y=130)
# ---
label = tk.Label(parison_window, text="1")
label.place(x=10, y=140 20)
entry = tk.Entry(parison_window, width=5, justify="center")
entry.place(x=30, y=140 20)
error_text = None # global variable with default value
# ---
save_button = tk.Button(parison_window, text="save", command=check_and_save)
save_button.pack()
parison_window.mainloop()