blakewp
blakewp

Reputation: 13

Method of taking text into entry box and searching for it?

I'm trying to be able enter information into the entry box and then have a function read it and return a statement depending on what was entered. Every time I run the program and try it, I get a failure that says "Type Error can only concatenate str (not "int) to str"

I've tried setting the text entry as a stringvar and then calling it as part of the function to search for that entry variable but it doesn't seem to change anything.

This is my code:

E = Entry(top,text='var',textvariable=var,bd=5)
def faultCodes():
    x = E.get()
    for x in E:
        if x == 500 or x == 514:
            print("Follow fault code 9621F4A4.")
        else:
            print("Enter a fault code.")
    label = Label(text=x).place(x=0,y=300)

The results I'm looking for is for a user to be able to enter a value of 500 or 514 and have the statement printed.

Upvotes: 1

Views: 59

Answers (1)

furas
furas

Reputation: 142909

You can create button to run code which will get text from var.get() or from E.get() and do something

E = Entry(top, text='var', textvariable=var, bd=5)
E.pack()

B = Button(top, text='OK', command=faultCodes)
B.pack()

def faultCodes():
    entry_text = var.get()

    if entry_text == "500" or entry_text == "514":
         print("Follow fault code 9621F4A4.")
    else:
         print("Enter a fault code.")

    label = Label(top, text=entry_text)
    label.pack()

Or you can bind <Return> to Entry to run function when you press Enter in Entry. Tkinter will run function with event as argument so function has to get this argument.

E = Entry(top, text='var', textvariable=var, bd=5)
E.pack()

E.bind('<Return>', faultCodes)

def faultCodes(event):
    entry_text = var.get()

    if entry_text == "500" or entry_text == "514":
         print("Follow fault code 9621F4A4.")
    else:
         print("Enter a fault code.")

    label = Label(top, text=entry_text)
    label.pack()

You can even use both method with the same function if you use event=None

Full working example

from tkinter import *

def faultCodes(event=None):
    entry_text = var.get()

    if entry_text == "500" or entry_text == "514":
         print("Follow fault code 9621F4A4.")
    else:
         print("Enter a fault code.")

    label = Label(top, text=entry_text)
    label.pack()


top = Tk()

var = StringVar()
E = Entry(top, text='var', textvariable=var, bd=5)
E.pack()

E.bind('<Return>', faultCodes)

B = Button(top, text='OK', command=faultCodes)
B.pack()

top.mainloop()

BTW: var.get() gives string so I compare with strings "500", "514" - not with integers 500, 514.


EDIT: example with Label

from tkinter import *

# --- function ---

def faultCodes(event=None):
    entry_text = var.get()

    if entry_text == "500" or entry_text == "514":
         label['text'] = "Follow fault code 9621F4A4."
    else:
         label['text'] = "Enter a fault code."

# --- main ---

top = Tk()

var = StringVar()
E = Entry(top, text='var', textvariable=var, bd=5)
E.pack()

E.bind('<Return>', faultCodes)

B = Button(top, text='OK', command=faultCodes)
B.pack()

label = Label(top, text='')
label.pack()

top.mainloop()

EDIT: example with dictionary

from tkinter import *

data = {
    "500": "Follow fault code 9621F4A4.",
    "514": "Follow fault code 9621F4A4.",
    # ...add more ...
}

# --- function ---

def faultCodes(event=None):
    entry_text = var.get()

    if entry_text in data:
         label['text'] = data[entry_text]
    else:
         label['text'] = "Enter a fault code."

# --- main ---

top = Tk()

var = StringVar()
E = Entry(top, text='var', textvariable=var, bd=5)
E.pack()

E.bind('<Return>', faultCodes)

B = Button(top, text='OK', command=faultCodes)
B.pack()

label = Label(top, text='')
label.pack()

top.mainloop()

Upvotes: 1

Related Questions