首页 > 解决方案 > 删除内容条目 tkinter 不起作用

问题描述

我的按钮有问题(清除)我尝试了很多方法,但都没有成功,我搜索了这个问题,但我没有找到解决问题的方法,所以我想在我清除条目中的内容时按下按钮清除。

以前,从我尝试过的方式来看:

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

标签: pythonuser-interfacetkinterwidgettkinter-entry

解决方案


您没有重新启用它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()

推荐阅读