Search code examples
pythonpython-3.xtkinteruser-inputtkinter-entry

Tkinter User Input Response


I'm currently trying to make a program that takes in user input and then based on user input will compare it to another string and print out a certain output if it matches in tkinter. I have attempted to use the get method, textvariable etc., I have also looked elsewhere but it doesn't seem to work or is outdated.

def trebleBass():
    print(username.get())
username = StringVar()
entry1 = Entry(window, textvariable = username)
entry2 = Entry(window)
logo = PhotoImage(file = "templogo2.png")
titleLogo = Label(window, image = logo)
titleLogo.grid(columnspan = 2)
framepackage = Frame(window)
framepackage.grid(row = 3)


label1.grid(row = 1, sticky = E)
label2.grid(row = 2, sticky = E)

entry1.grid(row = 1, column = 1)
entry2.grid(row = 2, column = 1)

translate = Button(window, text = "Translate", bg = 'black', fg = 'white', 
command = trebleBass())

I placed a function that would just print user input just to test it out but that doesn't even work. I am somewhat new to Python, so the help is highly appreciated.


Solution

  • Don't put parentheses on your command argument. When you put the parentheses, your passing in the return value of trebleBass (None in this case) instead of the function itself.

    Just put:

    translate = Button(window, text="Translate", bg="black", fg="white",
                       command=trebleBass)
    

    Notice that the parentheses are gone at the end.