2017-10-07 16 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 =ラベル(root、text = code) – 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

関連する問題