Как удалить лэйбл в ткинтере при вызове функции?
Я изучаю питон 3, и решил сделать небольшую программу которая решала бы квадратные уравнения с дискриминантом. Все работает и она решает, но лейблы накидываются друг на друга когда нажимаю на кнопку несколько раз, я пытался удалять лейблы, но не сработало, всегда что-то мешает. Я прикрепил код, простите что он не очень организован, но это не главная проблема, пожалуйста подскажите как мне надо удалять лейблы чтобы не было ошибки типа name x_label is not defined, я много всего пытался сделать и ничего не получается. Задавайте вопросы если надо что-то уточнить.
import tkinter as tk
from tkinter import *
from tkinter import messagebox
root = tk.Tk()
a_label = Label(root, text="Enter the value of a: ")
a_entry = Entry(root, borderwidth=4)
b_label = Label(root, text="Enter the value of b: ")
b_entry = Entry(root, borderwidth=4)
c_label = Label(root, text="Enter the value of c: ")
c_entry = Entry(root, borderwidth=4)
a_label.grid(row=0, column=0)
a_entry.grid(row=0, column=1)
b_label.grid(row=1, column=0)
b_entry.grid(row=1, column=1)
c_label.grid(row=2, column=0)
c_entry.grid(row=2, column=1)
def start():
global a, b, c, D, x_label, x12_label
a = a_entry.get()
b = b_entry.get()
c = c_entry.get()
D = float(b) * float(b) - 4 * float(a) * float(c)
if D < 0:
d_less0()
elif D == 0:
d_0()
else:
d_more0()
def d_less0():
error_msg = messagebox.showerror("Error", "No roots, because D is less than 0")
def d_0():
x = float(b) / -1 / (2 * float(a))
x_label = Label(text="D is equal to 0, therefore only one root, which is equal to " + str(x))
x_label.grid(row=6, column=2)
def d_more0():
x1 = (float(b) / -1 + sqrt(D)) / (2 * float(a))
x2 = (float(b) / -1 - sqrt(D)) / (2 * float(a))
x12_label = Label(root, text="Found two roots, x1 = " + str(x1) + " x2 = " + str(x2))
x12_label.grid(row=6, column=2)
check_btn = Button(root, text="Solve", command=start)
check_btn.grid(row=3, column=1)
root.mainloop()
Ответы (1 шт):
Автор решения: cyr oil
→ Ссылка
Создайте переменные
x_label=0
x12_label=0
а далее в функциях
global x_label
global x12_label