Mohammad Al Jadallah
Mohammad Al Jadallah

Reputation: 486

delete contents entry tkinter does not work

I have a problem with a button (Clear) I tried a lot of ways and it did not work and I search about this problem but I did not find the solution to my problem, so I want to clear the contents from the entry when I press on the button Clear.

previous, from the ways I tried it :

entry.delete(0, END)
entry.delete(0, 'end')
entry.insert(index, "  ")

and a lot of ways, so if you have the solution give me that, and thank you a lot in advanced

and this is my code:

from tkinter import *
from tkinter import ttk
import pyperclip as clip

root = Tk()
root.geometry('350x500')
root.title("Calculator")
root.configure(bg="#2D3A3E")
root.resizable(False, False)
largeFont = ('Comic Sans MS', 20)
fontButton = ('Comic Sans MS', 20)
style = ttk.Style()
i = 0

def press(x):
    global i
    i += 1
    entry.configure(state=NORMAL)
    entry.insert(i, x)  # المشكلة هنا أننا عندما نستخدم insert(index, value
    entry.configure(state=DISABLED)

def equalResult():
    pass

def copyNum():
    getText = entry.get()
    clip.copy(getText)
    clip.paste()
    root.update()


def clear():
    entry.delete(0, END)

# create buttons and entry
#('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')



entry = Entry(root, textvariable=largeFont, font=largeFont, bg='#2D3A3E', fg="#D6DFE0")
entry.place(width=400, height=100, y=0)


#All Buttons here 19:
style.theme_use('alt')
style.configure("TButton", foreground="#D6DFE0", font=fontButton, background='#2D3A3E')

zero = ttk.Button(root, text='0', command=lambda: press(0))
zero.place(width=75, height=60, y=440, x=0)

dot = ttk.Button(root, text='.', command=lambda: press('.'))
dot.place(width=75, height=60, y=440, x=75)

one = ttk.Button(root, text='1', command=lambda: press(1))
one.place(width=75, height=60, y=380, x=0)

two = ttk.Button(root, text='2', command=lambda: press(2))
two.place(width=75, height=60, y=380, x=75)

three = ttk.Button(root, text='3', command=lambda: press(3))
three.place(width=75, height=60, y=380, x=150)

four = ttk.Button(root, text='4', command=lambda: press(4))
four.place(width=75, height=60, y=320, x=0)

five = ttk.Button(root, text='5', command=lambda: press(5))
five.place(width=75, height=60, y=320, x=75)

six = ttk.Button(root, text='6', command=lambda: press(6))
six.place(width=75, height=60, y=320, x=150)

seven = ttk.Button(root, text='7', command=lambda: press(7))
seven.place(width=75, height=60, y=260, x=0)

eight = ttk.Button(root, text='8', command=lambda: press(8))
eight.place(width=75, height=60, y=260, x=75)

nine = ttk.Button(root, text='9', command=lambda: press(9))
nine.place(width=75, height=60, y=260, x=150)

equal = Button(root, text='=', fg="#D6DFE0", bg="green", font=("Comic Sans MS", 30, 'bold'), 
command=lambda: press('='))
equal.place(width=200, height=60, y=440, x=150)

plus = Button(root, text='+', fg="#D6DFE0", bg="green", font=("Comic Sans MS", 30,'bold'), 
command=lambda: press('+'))
plus.place(width=125, height=60, y=380, x=225)

minus = Button(root, text='ـــ', fg="#D6DFE0", bg="green", font=("arial", 30,'bold'), 
command=lambda: press('-'))
minus.place(width=125, height=60, y=320, x=225)

multiply = Button(root, text='x', fg="#D6DFE0", bg="green", font=("arial", 30, 'bold'), 
command=lambda: press('*'))
multiply.place(width=125, height=60, y=260, x=225)

mod = ttk.Button(root, text='%', command=lambda: press('%'))
mod.place(width=75, height=60, y=200, x=150)

# btn clear all text in the entry

Clear = ttk.Button(root, text='C', command=lambda: clear())
Clear.place(width=75, height=60, y=200, x=75)

# button remove text from the end

remove = ttk.Button(root, text='rem')
remove.place(width=75, height=60, y=200, x=0)

divide = Button(root, text='/', fg="#D6DFE0", bg="green", font=("arial", 30,'bold'), 
command=lambda: press('/'))
divide.place(width=125, height=60, y=200, x=225)

# btn color mode

colorMode = Button(root, text='color mode', fg="#D6DFE0", bg="orange", font=("arial", 
25,'bold'))
colorMode.place(width=225, height=40, y=160, x=1)

# btn copy

Copy = Button(root, text='copy', fg="#D6DFE0", bg="GREEN", font=("arial", 15,'bold'), 
command=lambda: copyNum())
Copy.place(width=125, height=40, y=160, x=225)


entry.configure(state=DISABLED)

mainloop()

Upvotes: 0

Views: 532

Answers (1)

imxitiz
imxitiz

Reputation: 3987

You aren't re enabling that entry. You had disabled that entry from here and not enabling. So, you are facing this issue:

def clear():
    entry.configure(state=NORMAL) 
    entry.delete(0, END)
    entry.configure(state=DISABLED)

Doing this should solve your problem.

And additionally you don't have to use other library just to copy text from entry, you can use this:

def copy_button():
    clip = Tk()
    clip.withdraw()
    clip.clipboard_clear()
    entry.configure(state=NORMAL)
    text=entry.get()
    entry.configure(state=DISABLED)
    clip.clipboard_append(text)  
    clip.destroy()

Upvotes: 1

Related Questions