Home > Software design >  how to use user input from tkinter with logic
how to use user input from tkinter with logic

Time:06-16

i am new to coding and am trying to make a random tv show selector with tkinter, i am trying to take the data typed into the text box, and dependent on that perform an action, i have been getting the error message: 'str' object is not callable

but i am not sure why

here is the code-

from tkinter import *
root = Tk()
root.geometry('1000x1000')

entry1 = Entry(root, width = 20)
entry1.pack()

text = entry1.get()

if text('Comedy'):
    print('Friends')





root.mainloop()

CodePudding user response:

Use the double equals sign for comparison. The correct if statement would be:

if text == 'Comedy':
    print('Friends')

(As an explanarion: when you put parenthesis behind a name, python assumes it's a function and tries to call it. Thus the erroe message: "'str' object is not callable"

CodePudding user response:

I added the function of submit and added another button.

from tkinter import *

root = Tk()
root.geometry("600x400")

text_var = StringVar()


def submit():
    name = text_var.get()    
    if name == 'Comedy':  
       print('Friends')
    else:
        print('Nope')
    text_var.set("")
    
        
(name_label := Label(root, text='Text', font=('calibre',10, 'bold')))
name_entry = Entry(root,textvariable = text_var, font=('calibre',10,'normal'))

sub_btn = Button(root,text = 'Submit', command = submit)

name_label.grid(row=0,column=0)
name_entry.grid(row=0,column=1)
 
sub_btn.grid(row=2,column=1)

root.mainloop()
  • Related