Python Tkinter- Direct pointer back to Entry() box
Question: When A user inputs a blank string of text I can either pop up a new input box
which looks nasty or, like a webpage, direct the cursor back into the Entry()
box

Unfortunately after searching I am still completely clueless as to how I can
achieve this direction of the cursor.

My code looks like this-

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

In the compatibility function I am wanting to change the `while Key == "":` to
pop-up a message (that's easy) and to direct the cursor back to the Key_Box( I
may also make it change to red or something)

So- does anyone know how I can achieve redirection of the cursor? Edit:I am
not sure whether this is included anywhere in Tkinter, I can use tab to switch
between Entry() boxes so I assume that they function in roughly the same way
as other entry boxes across different platforms.


Answer: You could call .focus() on the entry? It won't move the cursor, but the user
would be able to just start typing away in the entry box as if they had
clicked in it.

