【问题标题】:delete contents entry tkinter does not work删除内容条目 tkinter 不起作用
【发布时间】:2021-10-27 11:06:16
【问题描述】:

我的按钮有问题(清除)我尝试了很多方法,但都不起作用,我搜索了这个问题,但我没有找到解决问题的方法,所以我想从当我按下清除按钮时进入。

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

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

【问题讨论】:

    标签: python user-interface tkinter widget tkinter-entry


    【解决方案1】:

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

    【讨论】:

    • 创建一个新的Tk 有什么意义?您可以只使用entry.clipboard_clear()entry.clipboard_append()。我想不出你为什么要制作另一个窗口的任何理由。
    • 哦,非常感谢兄弟,这很好抱歉我没有注意到?。我曾经使用库 pyperclip 进行复制,因为当您复制并直接关闭程序时,剪贴板复制的文本不起作用。
    • 我尝试用 import os os.system('echo {}| clip.format(getText)) 替换剪贴板和 pyperclip,但程序会冻结到几秒钟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多