Skip to content
Advertisement

how do you ‘connect’ this source code with my tkinter python

hi am new to python and i have created a very simple encryption code and a simple gui with tkinter but i cant seem to ‘connect’ them together i tried so hard but i need help right now

def machine():
    global newMessage
    keys = "abcdefghijklmnopqrstuvwxyz !"
    values = keys[-1] + keys[0:-1]


    encryptDict = dict(zip(keys, values))
    decryptdict = dict(zip(values, keys))

    message = input("Enter your message so i can encrypt or decrypt it :) ")
    mode = input("encrypt(E) OR decrypt(D) ")
    if mode.upper() == "E":
        newMessage = ''.join([encryptDict[letter] for letter in message.lower()])
    elif mode.upper() == "D":
        newMessage = ''.join([decryptdict[letter] for letter in message.lower()])
    else:
        print("use either E or D ")

    return newMessage

print(machine())

that is the encryption method

import tkinter as tk

HIGHT = 240
WIDTH = 720

root = tk.Tk()

canvas = tk.Canvas(root, height=HIGHT, width=WIDTH)
canvas.pack()

label = tk.Label(root, text="Enter your message so i can encrypt or decrypt it :)")
label.place(relx=0.25, rely=0.1, relwidth=0.5, relheight=0.125)

entry = tk.Entry(root)
entry.place(relx=0.1, rely=0.3, relwidth=0.8, relheight=0.125)

entry = tk.Entry(root)
entry.place(relx=0.1, rely=0.7, relwidth=0.8, relheight=0.125)

button = tk.Button(root, text="Incrypt")
button.place(relx=0.68, rely=0.5, relwidth=0.125, relheight=0.125)

button = tk.Button(root, text="Decrypt")
button.place(relx=0.18, rely=0.5, relwidth=0.125, relheight=0.125)

root.mainloop()

and that is the gui

Advertisement

Answer

use the ‘command’ parameter in the Button widget to set it to an event handler which in your case is machine. Since you are encrypting and decrypting under the same function send a signal when calling this function as shown below using lambda function. Also, replace message = input("Enter your message so i can encrypt or decrypt it :) ") with message=entry.get(). You have assigned entry=Entry() twice I assume that’s a mistake so remove it. Finally instead of return use the existing label widget to display output using config or you can also create a new label and use that for displaying the output.

Here is the complete code, which you may further improvise:

import tkinter as tk

HIGHT = 240
WIDTH = 720

def machine(mode):
    global newMessage
    keys = "abcdefghijklmnopqrstuvwxyz !"
    values = keys[-1] + keys[0:-1]

    encryptDict = dict(zip(keys, values))
    decryptdict = dict(zip(values, keys))

    message = entry.get()

    if mode == 0:
        newMessage = ''.join([encryptDict[letter] for letter in message.lower()])

    elif mode == 1:
        newMessage = ''.join([decryptdict[letter] for letter in message.lower()])
    
    label.config(text='Message: '+ newMessage)

   
root = tk.Tk()

canvas = tk.Canvas(root, height=HIGHT, width=WIDTH)
canvas.pack()

label = tk.Label(root, text="Enter your message so i can encrypt or decrypt it :)")
label.place(relx=0.25, rely=0.1, relwidth=0.5, relheight=0.125)

entry = tk.Entry(root)
entry.place(relx=0.1, rely=0.3, relwidth=0.8, relheight=0.125)


button = tk.Button(root, text="Encrypt", command=lambda :machine(0))
button.place(relx=0.68, rely=0.5, relwidth=0.125, relheight=0.125)

button2 = tk.Button(root, text="Decrypt", command=lambda :machine(1))
button2.place(relx=0.18, rely=0.5, relwidth=0.125, relheight=0.125)

root.mainloop()

Update:

To insert the text into the Entry widget use Entry.insert(index, string). Also, don’t forget to clear the existing text in the Entry by using Entry.delete(0, END), the ‘END’ specifier will make sure that it deletes all the text starting from 0th index.

import tkinter as tk

HIGHT = 240
WIDTH = 720

def machine(mode):
    global newMessage
    keys = "abcdefghijklmnopqrstuvwxyz !"
    values = keys[-1] + keys[0:-1]

    encryptDict = dict(zip(keys, values))
    decryptdict = dict(zip(values, keys))

    message = entry.get()

    if mode == 0:
        newMessage = ''.join([encryptDict[letter] for letter in message.lower()])

    elif mode == 1:
        newMessage = ''.join([decryptdict[letter] for letter in message.lower()])

    entry2.delete(0, tk.END)
    entry2.insert(0,'Message: '+ newMessage)

   
root = tk.Tk()

canvas = tk.Canvas(root, height=HIGHT, width=WIDTH)
canvas.pack()

label = tk.Label(root, text="Enter your message so i can encrypt or decrypt it :)")
label.place(relx=0.25, rely=0.1, relwidth=0.5, relheight=0.125)

entry = tk.Entry(root)
entry.place(relx=0.1, rely=0.3, relwidth=0.8, relheight=0.125)

entry2 = tk.Entry(root)
entry2.place(relx=0.1, rely=0.7, relwidth=0.8, relheight=0.125)

button = tk.Button(root, text="Encrypt", command=lambda :machine(0))
button.place(relx=0.68, rely=0.5, relwidth=0.125, relheight=0.125)

button2 = tk.Button(root, text="Decrypt", command=lambda :machine(1))
button2.place(relx=0.18, rely=0.5, relwidth=0.125, relheight=0.125)

root.mainloop()
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement