delete contents entry tkinter does not work



我有一个按钮(清除)的问题,我尝试了很多方法,它没有工作,我搜索这个问题,但我没有找到我的问题的解决方案,所以我想从条目清除的内容,当我按下按钮清除。

previous,从我尝试的方式来看:

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

和很多方法,如果你有解决方案给我,非常感谢你在高级

,这是我的代码:

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()

您没有启用entry。您已经从这里禁用了entry并且没有启用。所以,你正面临着这个问题:

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

这样做应该能解决你的问题。

另外,你不需要使用其他库来从条目中复制文本,你可以使用这个:

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()

最新更新