-1

When I run this no errors come up, but the button does not return anything

import tkinter
#imports the tkinter module

window = tkinter.Tk()
#creates window
window.geometry("675x300")
#sets window size
window.configure(background="#66FFFF")
#sets window background
window.title("Vigenere Cipher")
#Window title
window.wm_iconbitmap('favicon.ico')
#window logo
photo = tkinter.PhotoImage(file="vigciph12.gif")
#imports photo
w = tkinter.Label(window, image=photo)
#puts photo in window
w.pack()

lblInst = tkinter.Label(window, text="Encrypt Message Below.", bg="#66FFFF", font=("Helvetica", 16))
#Adds title
lblInst.pack()

lblphrase = tkinter.Label(window, text="Enter phrase to be encrypted:", bg="#66FFFF", font=("Helvetica", 10))
#tells user to enter phrase

phrases = str()
phrase = tkinter.Entry(window, textvariable = phrases )
#######adds box for user to submit reply##############(I suspect this may be the root of the problem)
lblphrase.pack()
phrase.pack()

lblkeyphrase = tkinter.Label(window, text="Enter keyword:", bg="#66FFFF", font=("Helvetica", 10))

keyphrases = str()
keyphrase = tkinter.Entry(textvariable = keyphrases)
##adds box for user to submit reply#########(I suspect this may be the root of the problem)
lblkeyphrase.pack()
keyphrase.pack()


def keyword_cipher(key, phrase):

            if len(phrase) > len(key):
                while len(phrase) > len(key):
                    length_to_add = len(phrase) - len(key)
                    key = key + key[0:length_to_add]
                    #adds words together so phrase is long enough

            elif len(phrase) < len(key):
                while len(phrase) < len(key):
                    length_to_sub = len(key) - (len(key) - len(phrase))
                    key = key[0:length_to_sub]


            else:
                pass

            #shift the characters
            shifted_phrase = ''
            for i in range(len(phrase)):
                new_letter = (ord(key[i]) - 96) + (ord(phrase[i]) - 96) + 96
                if new_letter > 122:
                    new_letter = chr(new_letter - 26)

                else:
                    new_letter = chr(new_letter)


                shifted_phrase = shifted_phrase + new_letter
            return shifted_phrase

            #shifts letters


            lbl.configure(text = (shifted_phrase))

            lbl = tkinter.Label(window, text="Nothing Yet", bg="#66FFFF", font=("Helvetica", 10))
            lbl.pack()
##############This could be the problem
def lol():
    keyword_cipher(keyphrases, phrases)




btn = tkinter.Button(window, text="Submit", fg="#66FFFF", bg="#383a39",command=lol())
btn.pack()



window.mainloop()
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
pea poi
  • 9
  • 1
  • See http://stackoverflow.com/q/8269096/3001761 or dozens of others... – jonrsharpe Jun 29 '15 at 20:51
  • Does this answer your question? [Why is my Button's command executed immediately when I create the Button, and not when I click it?](https://stackoverflow.com/questions/5767228/why-is-my-buttons-command-executed-immediately-when-i-create-the-button-and-no) – Karl Knechtel Sep 05 '22 at 07:29

1 Answers1

1
tkinter.Button(window, text="Submit", fg="#66FFFF", bg="#383a39",command=lol())
#                                                                           ↑↑

This makes the lol function execute immediately. So what you pass as the keyword argument to the tkinter.Button call is the return value of lol(). So instead of lol being the command that is executed when you click the button, the return value of lol() is used as the command.

But lol() doesn’t return anything:

def lol():
    keyword_cipher(keyphrases, phrases)

So there is no command bound to the button at all. Instead, pass the function itself:

tkinter.Button(…, command=lol)

Because there are no parentheses, it’s not called immediately. So the function is used as the command callback.

Note that you should do something with the return value of keyword_cipher(). For example show it somewhere in the UI.

poke
  • 369,085
  • 72
  • 557
  • 602