2017-10-07 36 views
0

我是新的python,並試圖製作一個程序,將給定的字符串轉換爲密碼。用戶在文本框中輸入的字符串作爲輸入並以祕密代碼轉換(使用加密模塊)。如何顯示結果在窗口(我嘗試使用的標籤,但它顯示了一個錯誤。)如何在tkinter窗口中添加函數結果字符串?

from tkinter import * 
import encryption as En    # Loading Custom libraries 
import decryption as De 
out_text = None  # Out text is the output text of message or the encryption 
root = None 
font_L1 = ('Verdana', 18, 'bold') # The font of the header label 
button1_font = ("Ms sans serif", 8, 'bold') 
button2_font = ("Ms sans serif", 8, 'bold') 
font_inst = ("Aerial", 8) 
my_text = None 
input_text = None 
text_box = None 
resut_l = None 
result_2 = None 

def b1_action():      # Encryption button 
    input_text = text_box.get() 
    if input_text == "": 
     print("Text field empty") 
    else: 
     En.enc_text(input_text)   # Message is returned as 'code' 

def b2_action(): 
    input_text = text_box.get() 
    if input_text == "": 
     print("Text field Empty") 
    else: 
     De.dec_text(input_text)   

def enc_button():   # Button for rendering encryption 
    b1 = Button(root, text = "ENCRYPT", font = button1_font, command = b1_action) 
    b1.configure(bg = 'palegreen3', width = '10', height = '3') 
    b1.place(x = '120', y = '130') 

def dec_button():   # Button for decryption 
    b2 = Button(root, text = "DECRYPT", font = button2_font, command = b2_action) 
    b2.configure(bg = 'palegreen3', width = '10', height = '3') 
    b2.place(x = '340', y = '130') 

def main():       #This is the core of GUI 
    global root 
    global text_box 
    root = Tk() 
    root.geometry("550x350") 
    root.configure(bg = "MediumPurple1") 
    win_text = Label(root, text = 'Enter text below and Choose an action:', bg = 'MediumPurple1', font = font_L1) 
    win_text.place(x = '10', y = '50') 
    text_box = Entry(root, text = 'Enter the Text', width = 60, bg = 'light blue') 
    text_box.place(x = '100', y = '100') 
    inst_text = Label(root, text = instructions, bg = "MediumPurple1", font = font_inst) 
    inst_text.pack(side = BOTTOM) 
    enc_button() 
    dec_button() 
    root.title('Secret Message.') 
    root.mainloop() 


main() 

這裏是加密模塊

def enc_text(line): 
    msg = str(line).replace(' ', '_').lower() 
    msg_list = list(msg) 
    all_char = list("[email protected]") 

    for i in range(0, len(msg)): 
     pos_replaced = all_char.index(str(msg_list[i])) #will give the positon of the word to be replaced in the main list of alphabets 
     msg_list.insert(i, all_char[pos_replaced + 3]) #will replace the elements one by one 
     msg_list.pop(i + 1) 
     i += 1 

    code = ''.join(msg_list).replace('@', ' ') 
    print(code) 

您也可以推薦一些即興。

+0

標籤是一種明智的做法。你遇到了什麼錯誤? – Kevin

+0

@Kevin Label小部件無法識別導入模塊中的結果(在本例中爲'code'變量)。我使用它,像l1 =標籤(根,文本=代碼) – Bing

+0

因此,精確的錯誤信息是'標籤小部件不識別結果'?我不認爲我對那個很熟悉。 – Kevin

回答

0

問題的部分原因是Entry部件沒有text=配置選項,因此它在該行完全忽視:

text_box = Entry(root, text='Enter the Text', width=60, bg='light blue') 

來處理Entry的字符內容的最好方法是使用它的textvariable=選項並將它的值設置爲tkinter.StringVar的一個實例,然後獲取並設置的值即對象將自動更新屏幕上的Entry小部件。

以下是您的代碼,對其進行了更改。注意我評論並改變了一些不相關的東西來運行代碼,但試圖指出最重要的東西。另請注意,我在您的定製encryption模塊的enc_text()函數的最後添加了一條return code聲明。

from tkinter import * 
import encryption as En    # Loading Custom libraries 
#import decryption as De   # DON'T HAVE THIS. 

out_text = None  # Out text is the output text of message or the encryption 
root = None 
font_L1 = ('Verdana', 18, 'bold') # The font of the header label 
button1_font = ("Ms sans serif", 8, 'bold') 
button2_font = ("Ms sans serif", 8, 'bold') 
font_inst = ("Aerial", 8) 
my_text = None 
input_text = None 
text_var = None      # ADDED. 
text_box = None 
resut_l = None 
result_2 = None 

# CHANGED TO USE NEW "text_var" variable. 
def b1_action():      # Encryption button 
    input_text = text_var.get() 
    if input_text == "": 
     print("Text field empty") 
    else: 
     text_var.set(En.enc_text(input_text)) 

def b2_action(): 
    input_text = text_box.get() 
    if input_text == "": 
     print("Text field Empty") 
    else: 
     """De.dec_text(input_text)""" 

def enc_button():   # Button for rendering encryption 
    b1 = Button(root, text="ENCRYPT", font=button1_font, command=b1_action) 
    b1.configure(bg='palegreen3', width='10', height='3') 
    b1.place(x='120', y='130') 

def dec_button():   # Button for decryption 
    b2 = Button(root, text="DECRYPT", font=button2_font, command=b2_action) 
    b2.configure(bg='palegreen3', width='10', height='3') 
    b2.place(x='340', y='130') 

def main():       #This is the core of GUI 
    global root 
    global text_box 
    global text_var     # ADDED 
    root = Tk() 
    root.geometry("550x350") 
    root.configure(bg="MediumPurple1") 
    win_text = Label(root, text='Enter text below and Choose an action:', 
        bg='MediumPurple1', font=font_L1) 
    win_text.place(x='10', y='50') 
    text_var = StringVar()   # ADDED 
    text_var.set('Enter the Text') # ADDED 
    # CHANGED text='Enter the Text' to textvariable=text_var 
    text_box = Entry(root, textvariable=text_var, width=60, bg='light blue') 
    text_box.place(x='100', y='100') 
    inst_text = Label(root, text="instructions", bg="MediumPurple1", 
         font=font_inst) 
    inst_text.pack(side=BOTTOM) 
    enc_button() 
    dec_button() 
    root.title('Secret Message.') 
    root.mainloop() 

main() 
+0

好。如果我的答案解決了您的問題,請接受它。請參閱[_當有人回答我的問題時該怎麼辦?_](http://stackoverflow.com/help/someone-answers) – martineau

相關問題