1

I am trying to clear or reset a text box and store new results. Basically, every time i press the button, I would like the text box to be emptied before the new text is stored.

In my research the following thread popped up and he seems to have a similar goal however I wasn't sure how to apply that to my code. TKinter - Clear text widget

I've tried the following but it doesn't work for obvious reasons. Not sure how else to approach this.

button = tk.Button(root, command=lambda: button_cmmd())

def button_cmmd():
    if len(text_widget_1.get("1.0", "end-1c")) <= 5:
        #I get descriptions - this portion works.
    else:
        text_widget_2.delete("1.0", END)

EDIT A half solution I've worked is:

def button_cmmd():
    if len(text_widget_2.get("1.0", "end-1c")) <= 2:
        text_widget_1.get("1.0", "end-1c"))
    else:
        text_widget_2.delete("1.0", "end-1c")

A minor annoyance now is that clicking the button once will populate the text_box_2, a 2nd click will delete that text so you have to click it a third time for it to run the .get again.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Blackmagic42
  • 83
  • 1
  • 8
  • Why do you think the code doesn't work? I don't see any obvious reason why it wouldn't work. – Bryan Oakley Apr 09 '21 at 17:41
  • @BryanOakley - The button works in two clicks however I was trying to make it clear any existing text and insert in 1 click. – Blackmagic42 Apr 09 '21 at 19:14
  • I don't understand the problem. Why not just remove the if statement and then run both functions? – Bryan Oakley Apr 09 '21 at 19:37
  • @BryanOakley - Wow, I expected that to work originally but, when it didn't, I started looking for alternative solutions...... I must have had something else throwing it off I guess. Thank you! – Blackmagic42 Apr 09 '21 at 22:01

1 Answers1

0

You could use a tkinter.StringVar object and pass it to the textvariable argument of the tkinter.Entry() call.

entry_var = tkinter.StringVar(root, value="default value")
entry = tkinter.Entry(root, textvariable=entry_var)
button = tkinter.Button(root, text="Clear", command=lambda: clear_entry(entry_var))

entry.pack()
button.pack()

def clear_entry(var):
    var.set("")

To test it, I just ran it in IDLE:

>>> import tkinter
>>> root = tkinter.Tk()
>>> entry_var = tkinter.StringVar(root, value="default value")
>>> def clear_entry(var):
    var.set("")

    
>>> entry = tkinter.Entry(root, textvariable=entry_var)
>>> button = tkinter.Button(root, text="Clear", command=lambda: clear_entry(entry_var))
>>> entry.pack()
>>> button.pack()
>>> root.mainloop()

Before clicking:

Before

After clicking:

After

Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
  • Jacob Lee - My button text is "Generate" and is currently setup to ```.get``` the multiline text from text widget 1, process it, and insert it back into text widget 2. My question is how do I build the ability to clear the text in text box 2 within my same button. i.e. I just want it to clear text widget 2 first and then insert the new data on every button click. – Blackmagic42 Apr 08 '21 at 11:56
  • In this case there's nothing you can do with a `StringVar` that you can't do directly with the entry widget. The introduction of the `StringVar` adds complexity but doesn't actually solve the problem. – Bryan Oakley Apr 09 '21 at 19:38