Delete all not working in tkinter

Post here if you need help with creating a Graphical User Interface in Python.

Delete all not working in tkinter

Postby nnani » Thu Jan 09, 2014 7:39 am

Hello All,

I am trying to work with a text widget using tkinter.
Below is the code snippet
Code: Select all
import tkinter as tk
import sys

class total_window(tk.Tk):
    "class including info and functions on gui window and system"

    def __init__(self):
        "initiating the instance of class"
        tk.Tk.__init__(self)
        "initiating the instance of ancestor class"
               
        toolbar = tk.Frame(self)
        toolbar.config(bg="blue")
        toolbar.pack(side="top", fill="x")
        b1 = tk.Button(self, text="Click Here")
        exit_button = tk.Button(self,text="Exit",bg="orange",command = self.destroy)       
        b1.pack(in_=toolbar,side ="left")
        exit_button.pack(in_=toolbar,side="right")       
        t1 = tk.Text(self, wrap="word")
        t1.pack(side="top", fill="both" , expand=True)
        t1.insert('1.0', 'my status here.\n')
        delete_button = tk.Button(self, text="Delete All", command = t1.delete(1.0, "end"))
        delete_button.pack(in_=toolbar,side="right")

demo = total_window()
demo.mainloop()


Executing the above code, The delete button is not working at all, I want my delete button to delete all the contents in the text widget,
Any links to what is the issue here.
nnani
 
Posts: 6
Joined: Tue Jan 07, 2014 10:23 am

Re: Delete all not working in tkinter

Postby octopez » Thu Jan 09, 2014 9:17 am

Take a look at how you've bound your event to the widget. The line
Code: Select all
delete_button = tk.Button(self, text="Delete All", command = t1.delete(1.0, "end"))

is not correct, as you can't use parenthesis with the command= paramaeter AFAIK.

You could extract the command to a new function or for small/short scripts use lambdas:
Code: Select all
        delete_button = tk.Button(self, text="Delete All", command = lambda: t1.delete(1.0, "end"))
Greetings and all the best,
Octopez

Strange women lying in ponds distributing swords is no basis for a system of government.
User avatar
octopez
 
Posts: 25
Joined: Fri Feb 08, 2013 11:26 pm

Re: Delete all not working in tkinter

Postby nnani » Thu Jan 09, 2014 10:26 am

Thanks for that.
I learned something new. :)
nnani
 
Posts: 6
Joined: Tue Jan 07, 2014 10:23 am


Return to GUI

Who is online

Users browsing this forum: W3C [Linkcheck] and 3 guests