Python Tkinter- 直接指向 Entry() 框的指针

Python Tkinter- Direct pointer back to Entry() box

当用户输入空白文本字符串时,我可以弹出一个看起来很讨厌的新输入框,或者像网页一样,将光标引导回 Entry() 框

不幸的是,经过搜索,我仍然完全不知道如何实现光标的这个方向。

我的代码是这样的-

import time
from tkinter import *
root = Tk()

##Encrypt and Decrypt
Master_Key = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#£$%&'()*+,-./:;?@[\]^_`{|}~\t\n\r\x0b\x0c"

def Encrypt(User_Input, Key):
    Output = ""
    for i in range(len(User_Input)): 
        Ref_For_Output = Master_Key.index(User_Input[i]) + Master_Key.index(Key[i])
        if Ref_For_Output >= len(Master_Key):        
            Ref_For_Output -= len(Master_Key)
        Output += Master_Key[Ref_For_Output]
    return Output 

def Decrypt(User_Input, Key):
    Output = ""
    for i in range(len(User_Input)):
        Ref_For_Output = Master_Key.index(User_Input[i]) - Master_Key.index(Key[i])
        if Ref_For_Output < 0:
            Ref_For_Output += len(Master_Key)
        Output += Master_Key[Ref_For_Output]
    return Output

##def popup():
##    main = Tk()
##    Label1 = Label(main, text="Enter a new key: ")
##    Label1.grid(row=0, column=0)
##    New_Key_Box = Entry(main, bg="grey")
##    New_Key_Box.grid(row=1, column=0)
##
##    Ok = Button(main, text="OK", command=Set_Key(New_Key_Box.get()))
##    
##    Ok.grid(row=2, column=0)
##    if 
##    main.geometry("100x300")
##    main.mainloop()
##    return New_Key_Box.get()

class MyDialog:

    def __init__(self, parent):

        top = self.top = Toplevel(parent)

        Label(top, text="Value").pack()

        self.e = Entry(top)
        self.e.pack(padx=5)

        b = Button(top, text="OK", command=self.ok)
        b.pack(pady=5)

    def ok(self):

        print( "value is" + self.e.get())
        return self.e.get()
        self.top.destroy()


def Compatibility(User_Input, Key):
    while Key == "":
        root = Tk()
        Button(root, text="Hello!").pack()
        root.update()

        d = MyDialog(root)
        print(d.ok(Key))
        root.wait_window(d.top)
    Temp = 0
    while len(Key) < len(User_Input): 
        Key += (Key[Temp])
        Temp += 1
    return Key


##Layout
root.title("A451 CAM2")
root.geometry("270x80")

Label1 = Label(root, text="Input: ")
Label1.grid(row=0, column=0, padx=10)

Label2 = Label(root, text="Key:   ")
Label2.grid(row=1, column=0, padx=10)

Input_Box = Entry(root, bg="grey")
Input_Box.grid(row=0, column=1)

Key_Box = Entry(root, bg="grey")
Key_Box.grid(row=1, column=1)

def Encrypt_Button_Press():
    User_Input = Input_Box.get()
    Key = Compatibility(User_Input, Key_Box.get())
    print(User_Input)
    root.clipboard_append(Encrypt(User_Input, Key))
    Encrypt_Button.configure(text="Encrypting")
    messagebox.showinfo("Complete", "Your encrypted text is: \n" + Encrypt(User_Input, Key) + "\n The text has been added to your clipboard.")
    Encrypt_Button.configure(text="Encrypt")
    #popup()

def Decrypt_Button_Press():
    User_Input = Input_Box.get()
    Key = Key = Compatibility(User_Input, Key_Box.get())
    print(User_Input)
    root.clipboard_append(Decrypt(User_Input, Key))
    Decrypt_Button.configure(text="Decrypting")
    messagebox.showinfo("Complete", "Your Decrypted text is: \n" + Decrypt(User_Input, Key) + "\n The text has been added to your clipboard.")
    Decrypt_Button.configure(text="Decrypt")

Encrypt_Button = Button(text="Encrypt", command=Encrypt_Button_Press)
Encrypt_Button.grid(row=0, column=3, padx=10)

Decrypt_Button = Button(text="Decrypt", command=Decrypt_Button_Press)
Decrypt_Button.grid(row=1, column = 3, padx=10)

root.mainloop() 

在兼容性功能中我想更改 while Key == "": 弹出一条消息(这很容易)并将光标引导回 Key_Box(我也可以将其更改为红色或其他内容)

那么-有谁知道如何实现光标重定向? Edit:I 我不确定这是否包含在 Tkinter 的任何地方,我可以使用选项卡在 Entry() 框之间切换,所以我假设它们的功能与不同平台上的其他输入框大致相同。

您可以在条目上调用 .focus() 吗?它不会移动光标,但用户可以开始在输入框中键入内容,就好像他们在其中单击了一样。