Home > Software engineering >  How do I get an input from an Entry in Tkinter to be used in a function to be used in another window
How do I get an input from an Entry in Tkinter to be used in a function to be used in another window

Time:06-02

# Imports the tkinter library.
from tkinter import *

# Pillow is a improved verison of PIL, stands for Python Image Library. Allows you to import image types like .jpg and .png
from PIL import ImageTk,Image

# Imports messagebox module from tkinter library.
from tkinter import messagebox

# Tk() helps to display the root window and manages all the other components of the tkinter application and assigns it to root.
root = Tk()
root.eval("tk::PlaceWindow . center")

# Renames the title of the tkinter window
root.title('d3cryptt')

def openCipher():
    cipher = Toplevel()
    cipher.title("decryptt - CIPHER")
    cipherLabel = Label(cipher, text="cipher").pack()
    cipherEntry = Entry(cipher, width=20, borderwidth=5).pack()
    quitButton = Button(cipher, text="Exit Cipher", padx=10, pady=5, command=cipher.destroy).pack()
    cipherButton = Button(cipher, text="Cipher", padx=10, pady=5, command=ciphering).pack()

def openDecipher():
    decipher = Toplevel()
    decipher.title("decryptt - DECIPHER")
    decipherLabel = Label(decipher, text="decipher").pack()
    quitButton = Button(decipher, text="Exit Decipher", padx=10, pady=5, command=decipher.destroy).pack()
    
# This is the function that is suppose to split the input from cipherEntry into individual characters in an array.
def ciphering(cipherEntry):
    seperatedWord = list(cipherEntry.get())
    cipherLabeling = Label(root, text = "You have inputted "   seperatedWord.get()).pack()

appLogo = ImageTk.PhotoImage(Image.open("d3cryptt_logo_resized.png"))
appLogoLabel = Label(image=appLogo)
appLogoLabel.grid(row=0, column=0, columnspan=2)

cipherButton = Button(root, text="  Cipher  ", padx=40, pady=20, command=openCipher).grid(row=1, column=0)
decipherButton = Button(root, text="Decipher", padx=40, pady=20, command=openDecipher).grid(row=1, column=1)
spacer1 = Label(root, text="     ", padx=10, pady=1).grid(row=4, column=1)
quitButton = Button(root, text="Exit d3cryptt", padx=10, pady=5, command=root.quit).grid(row=5, column=0, columnspan=2)
spacer2 = Label(root, text="     ", padx=10, pady=1).grid(row=6, column=1)

root.mainloop()

(This is all I have so far and have included the functions and the entry field)

This is the code I have for my ciphering app.

I am trying to get the input from cipherEntry to be used in the function ciphering. I then want to create an array from cipherEntry that holds each individual character of cipherEntry using list(). I want this array to be assigned to seperatedWord.

I have also tried assigning the input to another variable using .get() and then tried to input it into the function ciphering.

However, I can't get the input of cipher entry to be put into the function.

Could I get some help on how to do this?

CodePudding user response:

I added notes throughout the answer to explain my changes

This should work:

# Imports the tkinter library.
from tkinter import *

# Pillow is a improved verison of PIL, stands for Python Image Library. Allows you to import image types like .jpg and .png
from PIL import ImageTk,Image

# Imports messagebox module from tkinter library.
from tkinter import messagebox

# Tk() helps to display the root window and manages all the other components of the tkinter application and assigns it to root.
root = Tk()
root.eval("tk::PlaceWindow . center")

# Renames the title of the tkinter window
root.title('d3cryptt')

def openCipher():
    cipher = Toplevel()
    cipher.title("decryptt - CIPHER")
    cipherLabel = Label(cipher, text="cipher").pack()
    cipherEntry = Entry(cipher, width=20, borderwidth=5) #separating pack now allows you to use get() on this
    cipherEntry.pack()
    quitButton = Button(cipher, text="Exit Cipher", padx=10, pady=5, command=cipher.destroy).pack()
    cipherButton = Button(cipher, text="Cipher", padx=10, pady=5, command=lambda:ciphering(cipherEntry.get())).pack() #lambda allows you to pass arguments to functions

def openDecipher():
    decipher = Toplevel()
    decipher.title("decryptt - DECIPHER")
    decipherLabel = Label(decipher, text="decipher").pack()
    quitButton = Button(decipher, text="Exit Decipher", padx=10, pady=5, command=decipher.destroy).pack()
    
# This is the function that is suppose to split the input from cipherEntry into individual characters in an array.
def ciphering(entry)
    newTopLevel = Toplevel() #needed to add new label to
    cipherLabeling = Label(newTopLevel, text = "You have inputted "   entry).pack() #couldn’t add a list to string like that, nor use get() on a list, changed to just use the string

appLogo = ImageTk.PhotoImage(Image.open("d3cryptt_logo_resized.png"))
appLogoLabel = Label(image=appLogo)
appLogoLabel.grid(row=0, column=0, columnspan=2)

cipherButton = Button(root, text="  Cipher  ", padx=40, pady=20, command=openCipher).grid(row=1, column=0)
decipherButton = Button(root, text="Decipher", padx=40, pady=20, command=openDecipher).grid(row=1, column=1)
spacer1 = Label(root, text="     ", padx=10, pady=1).grid(row=4, column=1)
quitButton = Button(root, text="Exit d3cryptt", padx=10, pady=5, command=root.quit).grid(row=5, column=0, columnspan=2)
spacer2 = Label(root, text="     ", padx=10, pady=1).grid(row=6, column=1)

root.mainloop()
  • Related