2015-01-31 32 views
0

當用戶輸入空白字符串文本時,我可以彈出一個看起來很討厭的新輸入框,或者像網頁一樣將光標指回Entry()盒子Python Tkinter-直接將指針返回到條目()框

不幸的是,搜索後我仍然完全無能爲力,至於如何實現光標的這個方向。

我的代碼看起來像這個 -

import time 
from tkinter import * 
root = Tk() 

##Encrypt and Decrypt 
Master_Key = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#£$%&'()*+,-./:;[email protected][\\]^_`{|}~\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兼容性功能(我也可能會讓它變成紅色或其他東西)

所以 - 有誰知道我如何實現光標的重定向? 編輯:我不確定這是否包含在Tkinter中的任何位置,我可以使用Tab切換Entry()框,因此我假定它們的功能與其他跨平臺的輸入框大致相同。

回答

1

你可以在條目上調用.focus()?它不會移動光標,但用戶可以在輸入框中開始輸入,就好像他們已經點擊了一樣。

+0

是的。我正是這個意思。我會嘗試 – 2015-02-01 16:48:17