How to clear/delete the contents of a Tkinter Text widget?

后端 未结 7 1316
谎友^
谎友^ 2020-12-09 01:05

I am writing a Python program in TKinter on Ubuntu to import and print the name of files from particular folder in Text widget. It is just adding f

相关标签:
7条回答
  • 2020-12-09 01:29

    I think this:

    text.delete("1.0", tkinter.END)
    

    Or if you did from tkinter import *

    text.delete("1.0", END)
    

    That should work

    0 讨论(0)
  • 2020-12-09 01:36

    I checked on my side by just adding '1.0' and it start working

    tex.delete('1.0', END)
    

    you can also try this

    0 讨论(0)
  • 2020-12-09 01:39

    According to the tkinterbook the code to clear a text element should be:

    text.delete(1.0,END)
    

    This worked for me. source

    It's different from clearing an entry element, which is done like this:

    entry.delete(0,END) #note the 0 instead of 1.0

    0 讨论(0)
  • 2020-12-09 01:42
    from Tkinter import *
    
    app = Tk()
    
    # Text Widget + Font Size
    txt = Text(app, font=('Verdana',8))
    txt.pack()
    
    # Delete Button
    btn = Button(app, text='Delete', command=lambda: txt.delete(1.0,END))
    btn.pack()
    
    app.mainloop()
    

    Here's an example of txt.delete(1.0,END) as mentioned.

    The use of lambda makes us able to delete the contents without defining an actual function.

    0 讨论(0)
  • 2020-12-09 01:45

    A lot of answers ask you to use END, but if that's not working for you, try:

    text.delete("1.0", "end-1c")

    0 讨论(0)
  • 2020-12-09 01:47

    for me "1.0" didn't work, but '0' worked. This is Python 2.7.12, just FYI. Also depends on how you import the module. Here's how:

    import Tkinter as tk
    window = tk.Tk()
    textBox = tk.Entry(window)
    textBox.pack()
    

    And the following code is called when you need to clear it. In my case there was a button Save that saves the data from the Entry text box and after the button is clicked, the text box is cleared

    textBox.delete('0',tk.END)
    
    0 讨论(0)
提交回复
热议问题