1

I am a beginner. Can I ask you some help?

How can I clear an entry in Python tkinter. I can't clear it. I tried Parameters such as END and 'END' But I can't clear the entry when I Press a button

Can you answer as soon as possible thanks. I am working in a project now

This is my code:

from tkinter import *
class TkinScreen:
    def main_screen_opening(self):
        self.screen = Tk()
        self.screen.geometry ('500x500')
        self.screen.title ('Rwooggle Account')
        my_heading1 = Label (text = 'Rwooggle.com', bg = 'blue', fg = 'yellow', font = ('Courier', 34, 'italic'), width = '500').pack()
        my_heading = Label (text = 'Login', bg = 'blue', fg = 'black', font = ('Courier', 24, 'bold'), width = '500').pack()

    def variables(self):
        self.username_var = StringVar()
        self.password_var = StringVar()
        self.age_var = IntVar()
        self.city_var = StringVar()

    def labels_entries(self):
        self.username_text = Label (text = 'Username*', font = ('Courier', 18)).pack()
        self.username_entry = Entry (textvariable = self.username_var, width = '30').pack()
        Label (text = "", width = '30').pack()
        
        self.password_text = Label (text = 'Password*', font = ('Courier', 18)).pack()
        self.password_entry = Entry (textvariable = self.password_var, width = '30').pack()
        Label (text = "", width = '30').pack()
        
        self.age_text = Label (text = 'Age', font = ('Courier', 18)).pack()
        self.age_entry = Entry (textvariable = self.age_var, width = '30').pack()
        Label (text = "", width = '30').pack()

        self.city_text = Label (text = 'Country', font = ('Courier', 18)).pack()
        self.city_entry = Entry (textvariable = self.city_var, width = '30').pack()
        Label (text = "", width = '30').pack()

    def button(self):
        log_in_but = Button (text = 'Log In', font = ('Courier', 20), command = self.button_command).pack()

    def button_command(self):
        print ('User Sucessfully Logged In')
        self.user_info = self.username_var.get()
        self.pass_info = self.password_var.get()
        self.pass_print = '*' * (len(self.pass_info))
        self.age_info = self.age_var.get()
        self.city_info = self.city_var.get().capitalize()

        txt_info = f"""\
    Username    :   {self.user_info}
    Password    :   {self.pass_print}
    Age         :   {self.age_info}
    Country     :   {self.city_info}
        """
        print (txt_info)

        self.username_entry.delete(0, 'end')
        self.password_entry.delete(0, 'end')
        self.age_entry.delete(0, 'end')
        self.city_entry.delete(0, 'end')
        

    def function(self):
        self.screen.mainloop()

tkint = TkinScreen ()
tkint.main_screen_opening()
tkint.variables()
tkint.labels_entries()
tkint.button()
tkint.function()

This is the error User Sucessfully Logged In Username : FFNNFD Password : ****** Age : 12 Country : Eggreg

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Roger Geronimo\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Users\Roger Geronimo\Documents\Programming\SublimeText3\baf.py", line 53, in button_command
    self.username_entry.delete(0, 'end')
AttributeError: 'NoneType' object has no attribute 'delete'

I also tried the END Parameter

Thank you in advance

2 Answers2

0

Whatever entry you want to clear , use entry.set('') in a function which will be called when button is clicked

  • Is that an empty string? – The Noob Programmer Sep 05 '20 at 07:13
  • self.password_entry.set(' ') self.age_entry.set(' ') self.city_entry.set(' ') – The Noob Programmer Sep 05 '20 at 07:16
  • yeah it should work,your code also seems fine, kindly post you error as well, let me check –  Sep 05 '20 at 07:18
  • I used the same in my own project, I can help you out if you can post the error you are getting –  Sep 05 '20 at 07:25
  • ok. it shows NoneType Object has no attribute to set – The Noob Programmer Sep 05 '20 at 07:26
  • 'Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Roger Geronimo\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "C:\Users\Roger Geronimo\Documents\Programming\SublimeText3\baf.py", line 53, in button_command self.username_entry.set('') AttributeError: 'NoneType' object has no attribute 'set'' – The Noob Programmer Sep 05 '20 at 07:28
  • Well in that case is your `.get( )` working perfectly. Have you tried printing your entry input, as I have also faced the same error , and variables used to access the input in entry are locally defined and cannot access the input in entry. I recommend trying to print whatever you give in entry box –  Sep 05 '20 at 07:32
  • @RogerGeronimo The error is because you are using `set` on `self.username_entry` instead it should be on `self.username_var` like `self.username_var.set('')` but this is not the correct way to do so, i recommend my answer for the proper way. – Delrius Euphoria Sep 05 '20 at 08:23
0

Just change

self.username_entry = Entry (textvariable = self.username_var, width = '30').pack()

to

self.username_entry = Entry (textvariable = self.username_var, width = '30')
self.username_entry.pack()

I recommend to do this to all the entry boxes and then proceed as you normally do and it will solve the error :D

Explanation:

This is because self.username_entry = Entry (....).pack() returns None, ie, when you use self.username_entry.delete(0,'end') you are saying Entry (....).pack().delete(0,'end') which does not exist in the first place and since self.username_entry = Entry (....).pack() is None they give error, you received.

Extra tip:

  • I recommend using a master argument on your widgets, like, Entry(root) or when you work with more windows, itll cause havoc.
  • Instead of saying print ('User Sucessfully Logged In') you can use something like a messagebox with tkinter. Here is how

If you have any doubts, or errors, do let me know

Cheers

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46