1

What I want the frame to do is that when I click on the 'clear' button, the frame is cleaned but it does not and when I enter a string that is not valid and then a valid one, it shows traces of the past and past action. I already tried changing the Label.grid () by a Label.pack (), but it is worse since the 'animation' looks like a stack without removing any element when the 'clear' button is pressed

This is basically what would make it change

from tkinter import *
import tkinter.ttk as ttk

def clear():
    area.delete(0,END)
    frame.config(bd=1, relief=SUNKEN)
    frame.update()
    status = Label(frame) 
    status.grid(row=0, column=0, sticky=NSEW)


def statusVal(value):
    if not value == 0:
        status = Label(frame, background="#ff4242", fg="#262626", text="Cadena invalida", anchor="center") 
        status.grid(row=0, column=0)
        frame.config(bd=1, relief=SUNKEN, background="#ff4242")
        frame.update()
    else:
        status = Label(frame, background="#56ed42", fg="#262626", text="Cadena valida", anchor="center") 
        status.grid(row=0, column=0)
        frame.config(bd=1, relief=SUNKEN, background="#56ed42")
        frame.update()
#Test
def validation():
    capture = area.get()
    if capture == '1':
        return statusVal(0)
    else:
        return statusVal(1)

root = Tk()
root.geometry("300x150+300+300")

area = Entry(root)
area.grid(row=1, column=0, columnspan=2, sticky=E+W+S+N, padx=5)

frame = Frame(root, bd=1, relief=SUNKEN)
frame.grid(row=2, column=0, padx=5, pady=5, columnspan=2, sticky=W+E+S+N)
frame.columnconfigure(0,weight=5) 
frame.rowconfigure(0,weight=5)


abtn = Button(root, text="Validate", command=validation)
abtn.grid(row=1, column=3)

cbtn = Button(root, text="Clear", command=clear)
cbtn.grid(row=2, column=3, pady=5)

root.mainloop()
  • 1
    First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759). You are creating widgets over and over again. – stovfl Apr 06 '19 at 09:39
  • @stovfl Yes, I have not yet understood how to make that happen, I had checked a similar way but I still do not understand what to do to make it work, I have already tried with object.config () and after object.update () but I do not understand It's been less than a week that I'm starting with tkinter. – Javier Gaona Apr 06 '19 at 12:00
  • Javier: Although I see several (non-fatal) issues with your code, I can't reproduce the problem you say you are having — assuming that what you meant by "the frame is cleaned" is that the `status` `Label` and `area` `Entry` are emptied when the `"Clear"` button is clicked. – martineau Apr 06 '19 at 16:58
  • @martineau: look at this is the process I want to do and the problem. https://imgur.com/a/XD2dnLR – Javier Gaona Apr 06 '19 at 20:10
  • @martineau: what I want to do is so simple that it has already gone out of control. It is to simulate a frame that indicates if the string is valid or not. I expect feedback, I'm still not familiar with the functions – Javier Gaona Apr 06 '19 at 21:52
  • @martineau: no problem, thanks for the help. I was looking for more in depth and I managed to find something similar but when implementing it to the code I get this error `UnboundLocalError: local variable' status 'referenced before assignment`, any idea that could be or how to fix it? I've been looking for solutions for a while but I can not find. – Javier Gaona Apr 06 '19 at 23:59
  • That error usually means code is executing that references the variable `status` before it has been defined. See the code in the answer I just posted which avoids that — making it always exist also help avoid the issue. – martineau Apr 07 '19 at 00:02
  • @martineau: The aforementioned error was to do something similar to your proposed response, but very far from functioning. Even so, thanks for the feedback, I had not thought of structuring the `Frame` with the `Label`. – Javier Gaona Apr 07 '19 at 00:50

1 Answers1

0

See if this works better. The main change was to have the status Label always exist and hide or unhide it as desired — instead of creating a new one every time the validation() function was called. I also removed the code that was explicitly updating the frame which isn't necessary.

from tkinter import *
import tkinter.ttk as ttk


def clear():
    area.delete(0,END)
    status.grid_remove()  # Hide. but remember grid options.

def statusVal(value):
    if not value == 0:
        status.config(background="#ff4242", fg="#262626", text="Cadena invalida",
                      anchor="center")
        status.grid() # Unhide
    else:
        status.config(background="#56ed42", fg="#262626", text="Cadena valida",
                      anchor="center")
        status.grid() # Unhide

#Test
def validation():
    capture = area.get()
    if capture == '1':
        statusVal(0)
    else:
        statusVal(1)

# Main
root = Tk()
root.geometry("300x150+300+300")

area = Entry(root)
area.grid(row=1, column=0, columnspan=2, sticky=E+W+S+N, padx=5)

frame = Frame(root, bd=1, relief=SUNKEN)
frame.grid(row=2, column=0, padx=5, pady=5, columnspan=2, sticky=W+E+S+N)
frame.columnconfigure(0,weight=5)
frame.rowconfigure(0,weight=5)

# Initialize status Label.
status = Label(frame, anchor="center")
status.grid(row=0, column=0)
status.grid_remove()  # Hide it.

abtn = Button(root, text="Validate", command=validation)
abtn.grid(row=1, column=3)

cbtn = Button(root, text="Clear", command=clear)
cbtn.grid(row=2, column=3, pady=5)

root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301