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
  update text variable on label with keypress knoxvilles_joker 5 5,245 Yesterday, 02:09 PM
Last Post: menator01
  Button to +1 in text box everytime it's clicked martyloo 1 522 May-01-2024, 02:32 PM
Last Post: Axel_Erfurt
  Transparent window background, but not text on it muzicman0 7 3,226 Feb-02-2024, 01:28 AM
Last Post: Joically
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 1,015 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  [Kivy] Create a function to store text fields and drop downs selection in KivyMD floxia 0 1,775 Dec-18-2022, 04:34 AM
Last Post: floxia
  [Tkinter] Updating tkinter text BliepMonster 5 6,601 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 15,274 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,992 Nov-04-2022, 09:04 PM
Last Post: malonn
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 1,671 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 1,063 Oct-27-2022, 09:29 PM
Last Post: random_nick

Forum Jump:

User Panel Messages

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