在多个 tkinter Entry 小部件中使用退格键
Using backspace across multiple tkinter Entry widgets
我在我之前的一个项目中制作了 wordle(使用 tkinter),现在我不想使用一个条目来进行猜测,而是想要 5 个链接在一起的条目,它们的工作方式类似于:
在图像中,您可以在这些“链接”条目之间退格。我弄清楚了跨条目的输入,但如何删除它们?
from tkinter import *
root = Tk()
root.geometry('400x400')
def testlen():
global textinentry1, textinentry2, textinentry3, textinentry4, textinentry5
textinentry1= entry1.get()
textinentry2= entry2.get()
textinentry3= entry3.get()
textinentry4= entry4.get()
textinentry5= entry5.get()
if len(textinentry1) >1 :
entry1.delete(0,END)
entry1.insert(0,textinentry1[0])
entry2.delete(0, END)
entry2.insert(0,textinentry1[1] )
entry2.focus_set()
if len(textinentry2) >1 :
entry2.delete(0,END)
entry2.insert(0,textinentry2[0])
entry3.delete(0, END)
entry3.insert(0,textinentry2[1] )
entry3.focus_set()
if len(textinentry3) >1 :
entry3.delete(0,END)
entry3.insert(0,textinentry3[0])
entry4.delete(0, END)
entry4.insert(0,textinentry3[1] )
entry4.focus_set()
if len(textinentry4) >1 :
entry4.delete(0,END)
entry4.insert(0,textinentry4[0])
entry5.delete(0, END)
entry5.insert(0,textinentry4[1] )
entry5.focus_set()
if len(textinentry5) > 1:
entry5.delete(0,END)
entry5.insert(0,textinentry5[0])
entry1 = Entry(root,width=3, font = ('Georgia 18'), justify=CENTER)
entry1.grid(row=0, column=0)
entry2 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry2.grid(row=0, column=1)
entry3 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry3.grid(row=0, column=2)
entry4 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry4.grid(row=0, column=3)
entry5 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry5.grid(row=0, column=4)
def loop():
testlen()
root.after(1,loop) # 1 is 1 millisecond. Here root.after method calls the loop
# function after 1 millisecond without crashing your code.
loop()
root.mainloop()
首先,您必须获得一些东西来注册按钮按下。为此,我导入了 keyboard
并使用 if keyboard.is_pressed("backspace"):
查看是否按下了退格键。
不幸的是,当一个人按下一个按钮时,我们会按住它几毫秒,所以对于计算机来说,我们会多次按下它。为了防止这种情况,我导入了 time
并发现使用 time.sleep(.15)
是最好的延迟,因为它阻止了我不小心按下它一次,而且我也可以按住它而不用代码慢慢擦除每个字符.
这可以用循环更好地压缩,但我的独立框代码是:
from tkinter import *
import keyboard, time
root = Tk()
root.geometry('400x400')
def testlen():
global textinentry1, textinentry2, textinentry3, textinentry4, textinentry5, whathasfocus
whathasfocus= root.focus_get()
textinentry1= entry1.get()
textinentry2= entry2.get()
textinentry3= entry3.get()
textinentry4= entry4.get()
textinentry5= entry5.get()
if whathasfocus == entry1:
if len(textinentry1) >= 1:
pass
elif len(textinentry1) == 0:
if keyboard.is_pressed("delete"):
time.sleep(.15)
entry1.delete(0,END)
entry1.focus_set()
if whathasfocus == entry2:
if len(textinentry2) >= 1:
pass
elif len(textinentry2) == 0:
if keyboard.is_pressed("backspace"):
time.sleep(.15)
entry2.delete(0,END)
entry1.focus_set()
if keyboard.is_pressed("delete"):
time.sleep(.15)
entry2.delete(0,END)
entry2.focus_set()
if whathasfocus == entry3:
if len(textinentry3) >= 1:
pass
elif len(textinentry3) == 0 :
if keyboard.is_pressed("backspace"):
time.sleep(.15)
entry3.delete(0,END)
entry2.focus_set()
if keyboard.is_pressed("delete"):
time.sleep(.15)
entry3.delete(0,END)
entry3.focus_set()
if whathasfocus == entry4:
if len(textinentry4) >= 1:
pass
elif len(textinentry4) == 0:
if keyboard.is_pressed("backspace"):
time.sleep(.15)
entry4.delete(0,END)
entry3.focus_set()
if keyboard.is_pressed("delete"):
time.sleep(.15)
entry4.delete(0,END)
entry4.focus_set()
if whathasfocus == entry5:
if len(textinentry5) >= 1:
pass
elif len(textinentry5) == 0:
if keyboard.is_pressed("backspace"):
time.sleep(.15)
entry5.delete(0,END)
entry4.focus_set()
if len(textinentry1) >1 :
entry1.delete(0,END)
entry1.insert(0,textinentry1[0])
entry2.delete(0, END)
entry2.insert(0,textinentry1[1] )
entry2.focus_set()
if len(textinentry2) >1 :
entry2.delete(0,END)
entry2.insert(0,textinentry2[0])
entry3.delete(0, END)
entry3.insert(0,textinentry2[1] )
entry3.focus_set()
if len(textinentry3) >1 :
entry3.delete(0,END)
entry3.insert(0,textinentry3[0])
entry4.delete(0, END)
entry4.insert(0,textinentry3[1] )
entry4.focus_set()
if len(textinentry4) >1 :
entry4.delete(0,END)
entry4.insert(0,textinentry4[0])
entry5.delete(0, END)
entry5.insert(0,textinentry4[1] )
entry5.focus_set()
if len(textinentry5) > 1:
entry5.delete(0,END)
entry5.insert(0,textinentry5[0])
entry1 = Entry(root,width=3, font = ('Georgia 18'), justify=CENTER)
entry1.grid(row=0, column=0)
entry1.focus_set()
entry2 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry2.grid(row=0, column=1)
entry3 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry3.grid(row=0, column=2)
entry4 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry4.grid(row=0, column=3)
entry5 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry5.grid(row=0, column=4)
def loop():
testlen()
root.after(1,loop) # 1 is 1 millisecond. Here root.after method call the loop function after 1 millisecond without crashing your code.
loop()
root.mainloop()
我在我之前的一个项目中制作了 wordle(使用 tkinter),现在我不想使用一个条目来进行猜测,而是想要 5 个链接在一起的条目,它们的工作方式类似于:
在图像中,您可以在这些“链接”条目之间退格。我弄清楚了跨条目的输入,但如何删除它们?
from tkinter import *
root = Tk()
root.geometry('400x400')
def testlen():
global textinentry1, textinentry2, textinentry3, textinentry4, textinentry5
textinentry1= entry1.get()
textinentry2= entry2.get()
textinentry3= entry3.get()
textinentry4= entry4.get()
textinentry5= entry5.get()
if len(textinentry1) >1 :
entry1.delete(0,END)
entry1.insert(0,textinentry1[0])
entry2.delete(0, END)
entry2.insert(0,textinentry1[1] )
entry2.focus_set()
if len(textinentry2) >1 :
entry2.delete(0,END)
entry2.insert(0,textinentry2[0])
entry3.delete(0, END)
entry3.insert(0,textinentry2[1] )
entry3.focus_set()
if len(textinentry3) >1 :
entry3.delete(0,END)
entry3.insert(0,textinentry3[0])
entry4.delete(0, END)
entry4.insert(0,textinentry3[1] )
entry4.focus_set()
if len(textinentry4) >1 :
entry4.delete(0,END)
entry4.insert(0,textinentry4[0])
entry5.delete(0, END)
entry5.insert(0,textinentry4[1] )
entry5.focus_set()
if len(textinentry5) > 1:
entry5.delete(0,END)
entry5.insert(0,textinentry5[0])
entry1 = Entry(root,width=3, font = ('Georgia 18'), justify=CENTER)
entry1.grid(row=0, column=0)
entry2 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry2.grid(row=0, column=1)
entry3 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry3.grid(row=0, column=2)
entry4 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry4.grid(row=0, column=3)
entry5 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry5.grid(row=0, column=4)
def loop():
testlen()
root.after(1,loop) # 1 is 1 millisecond. Here root.after method calls the loop
# function after 1 millisecond without crashing your code.
loop()
root.mainloop()
首先,您必须获得一些东西来注册按钮按下。为此,我导入了 keyboard
并使用 if keyboard.is_pressed("backspace"):
查看是否按下了退格键。
不幸的是,当一个人按下一个按钮时,我们会按住它几毫秒,所以对于计算机来说,我们会多次按下它。为了防止这种情况,我导入了 time
并发现使用 time.sleep(.15)
是最好的延迟,因为它阻止了我不小心按下它一次,而且我也可以按住它而不用代码慢慢擦除每个字符.
这可以用循环更好地压缩,但我的独立框代码是:
from tkinter import *
import keyboard, time
root = Tk()
root.geometry('400x400')
def testlen():
global textinentry1, textinentry2, textinentry3, textinentry4, textinentry5, whathasfocus
whathasfocus= root.focus_get()
textinentry1= entry1.get()
textinentry2= entry2.get()
textinentry3= entry3.get()
textinentry4= entry4.get()
textinentry5= entry5.get()
if whathasfocus == entry1:
if len(textinentry1) >= 1:
pass
elif len(textinentry1) == 0:
if keyboard.is_pressed("delete"):
time.sleep(.15)
entry1.delete(0,END)
entry1.focus_set()
if whathasfocus == entry2:
if len(textinentry2) >= 1:
pass
elif len(textinentry2) == 0:
if keyboard.is_pressed("backspace"):
time.sleep(.15)
entry2.delete(0,END)
entry1.focus_set()
if keyboard.is_pressed("delete"):
time.sleep(.15)
entry2.delete(0,END)
entry2.focus_set()
if whathasfocus == entry3:
if len(textinentry3) >= 1:
pass
elif len(textinentry3) == 0 :
if keyboard.is_pressed("backspace"):
time.sleep(.15)
entry3.delete(0,END)
entry2.focus_set()
if keyboard.is_pressed("delete"):
time.sleep(.15)
entry3.delete(0,END)
entry3.focus_set()
if whathasfocus == entry4:
if len(textinentry4) >= 1:
pass
elif len(textinentry4) == 0:
if keyboard.is_pressed("backspace"):
time.sleep(.15)
entry4.delete(0,END)
entry3.focus_set()
if keyboard.is_pressed("delete"):
time.sleep(.15)
entry4.delete(0,END)
entry4.focus_set()
if whathasfocus == entry5:
if len(textinentry5) >= 1:
pass
elif len(textinentry5) == 0:
if keyboard.is_pressed("backspace"):
time.sleep(.15)
entry5.delete(0,END)
entry4.focus_set()
if len(textinentry1) >1 :
entry1.delete(0,END)
entry1.insert(0,textinentry1[0])
entry2.delete(0, END)
entry2.insert(0,textinentry1[1] )
entry2.focus_set()
if len(textinentry2) >1 :
entry2.delete(0,END)
entry2.insert(0,textinentry2[0])
entry3.delete(0, END)
entry3.insert(0,textinentry2[1] )
entry3.focus_set()
if len(textinentry3) >1 :
entry3.delete(0,END)
entry3.insert(0,textinentry3[0])
entry4.delete(0, END)
entry4.insert(0,textinentry3[1] )
entry4.focus_set()
if len(textinentry4) >1 :
entry4.delete(0,END)
entry4.insert(0,textinentry4[0])
entry5.delete(0, END)
entry5.insert(0,textinentry4[1] )
entry5.focus_set()
if len(textinentry5) > 1:
entry5.delete(0,END)
entry5.insert(0,textinentry5[0])
entry1 = Entry(root,width=3, font = ('Georgia 18'), justify=CENTER)
entry1.grid(row=0, column=0)
entry1.focus_set()
entry2 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry2.grid(row=0, column=1)
entry3 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry3.grid(row=0, column=2)
entry4 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry4.grid(row=0, column=3)
entry5 = Entry(root, width=3, font = ('Georgia 18'), justify=CENTER)
entry5.grid(row=0, column=4)
def loop():
testlen()
root.after(1,loop) # 1 is 1 millisecond. Here root.after method call the loop function after 1 millisecond without crashing your code.
loop()
root.mainloop()