Python Forum
How to delete text from a tkinter Text widget?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to delete text from a tkinter Text widget?
#1
Hey everyone,
I'm a beginner in python and I am trying to create a very very simple text editor (simplified version of microsoft Word for example). I already created the Text zone and the menubar, but I want to set up the commands inside the menu. For example, I want the command "Nouveau" (which means "new" in french) to open a new file. I used the
.delete()
mod from tkinter widgets so this is the code:
import tkinter.filedialog
from tkinter import*

def nouveau():
    text1.delete(1.0,END)
    
def ouvrir():
    file=tkinter.filedialog.askopenfile(mode='r')
    fileContents=file.read()
    text1.delete(1.0,END)
    text1.insert(1.0,fileContents)

def save():
    file=tkinter.filedialog.asksaveasfile(mode='w')
    textoutput=text1.get(1.0,END)
    file.write(textoutput.rstrip())
    file.write('\n')


fenetre=Tk()

menubar=Menu(fenetre)
menu1=Menu(menubar,tearoff=0)
menu1.add_command(label="Nouveau",command=nouveau)
menu1.add_command(label="Ouvrir",command=ouvrir)
menu1.add_command(label="Enregistrer",command=save)
menu1.add_separator()
menu1.add_command(label="Quitter",command=fenetre.quit)
menubar.add_cascade(label="Fichier",menu=menu1)

text1=Text(fenetre, width=100,height=300).pack(side=BOTTOM,padx=30,pady=30)

fenetre.config(menu=menubar)
fenetre.mainloop()
But when I run it and click on the "Nouveau" command in the menu, I get this:

Error:
AttributeError: 'NoneType' object has no attribute 'delete'
Thanks for your help! Big Grin
Reply
#2
text1.delete('1.0', END)
text1.update()
this is what you have now, you may have to update the widget to see
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Type text is incorrect. ammb 1 2,438 Sep-07-2025, 02:36 AM
Last Post: deanhystad
Information [WxPython] [SOLVED] Let user search read-only text control? Winfried 3 1,277 Jun-24-2025, 02:04 PM
Last Post: deanhystad
  Trying to update label text using a grid button. Edward_ 7 3,748 Dec-18-2024, 03:05 AM
Last Post: Edward_
  [PyQt] Rotake rect, image or text at its center LavaCreeperKing 8 4,756 Oct-24-2024, 09:42 PM
Last Post: deanhystad
  [Tkinter] Text input OK on Windows, not working on linux Ota 3 2,207 Sep-19-2024, 12:02 AM
Last Post: Ota
  popup, text input with two readable buttons ethompson 7 3,791 Aug-28-2024, 03:40 AM
Last Post: deanhystad
  How to delete tkinter table items? theCarl 1 2,121 Jun-27-2024, 03:16 PM
Last Post: Gribouillis
  [PyQt] Populate QComboBox with "text" and "userData" from database. carecavoador 0 2,279 Jun-19-2024, 02:01 PM
Last Post: carecavoador
  update text variable on label with keypress knoxvilles_joker 5 9,660 May-31-2024, 02:09 PM
Last Post: menator01
  Button to +1 in text box everytime it's clicked martyloo 1 2,024 May-01-2024, 02:32 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020