I am making a program that searches a list of stock numbers(the first 8 digit int in the list) and then returns the rest of the values in the list inside the entry boxes but when I click the search button, it does not perform its function. Any advice? (I'm also new to coding, is there any place where I could've shortened my code anywhere to make it more efficient?)
root = Tk()
#lists
car = [19225735, '611926', '2018', 'Hyundai', 'Sonata', 'White', 'Recon', '$25,000',
'Sedan',32,123]
#funtion
def search():
x = stockNumber.get()
if (x == car[0]):
vinNumber.insert(car[1])
make.insert(car[3])
model.insert(car[4])
color.insert(car[5])
status.inset(car[6])
price.insert(car[7])
size.insert(car[8])
mileage.insert(car[9])
#text boxes --------------------------------------------
stockNumber = Entry(root, width=30)
stockNumber.grid(row=0, column=1, padx=20)
vinNumber = Entry(root, width=30)
vinNumber.grid(row=1, column=1, padx=20
year = Entry(root, width=30)
year.grid(row=2, column=1, padx=20)
make = Entry(root, width=30)
make.grid(row=3, column=1, padx=20)
model = Entry(root, width=30)
model.grid(row=4, column=1, padx=20)
color = Entry(root, width=30)
color.grid(row=5, column=1, padx=20)
status = Entry(root, width=30)
status.grid(row=6, column=1, padx=20)
price = Entry(root, width=30)
price.grid(row=7, column=1, padx=20)
size = Entry(root, width=30)
size.grid(row=8, column=1, padx=20)
mileage = Entry(root, width=30)
mileage.grid(row=8, column=1, padx=20)
#button command-------------------------------
enter = Button(root, text = "Search", padx=40, pady=20, command=search)
enter.grid(row=9, column=0)
#labels ------------------------------------------------
snLabel = Label(root, text="Stock Number")
snLabel.grid(row=0, column=0)
vnLabel = Label(root, text="Vin Number")
vnLabel.grid(row=1, column=0)
yearLabel = Label(root, text="Year")
yearLabel.grid(row=2, column=0)
makeLabel = Label(root, text="Make")
makeLabel.grid(row=3, column=0)
modelLabel = Label(root, text="Model")
modelLabel.grid(row=4, column=0)
colorLabel = Label(root, text="Color")
colorLabel.grid(row=5, column=0)
statusLabel = Label(root, text="Status")
statusLabel.grid(row=6, column=0)
sizeLabel = Label(root, text="Size")
sizeLabel.grid(row=7, column=0)
mileLabel = Label(root, text="Mileage")
mileLabel.grid(row=8, column=0)
The Button was working fine, your function was the problem:
def search():
x = int(stockNumber.get())
if (x == car[0]):
vinNumber.insert(0,str(car[1]))
make.insert(0,str(car[3]))
model.insert(0,str(car[4]))
color.insert(0,str(car[5]))
status.insert(0,str(car[6]))
price.insert(0,str(car[7]))
size.insert(0,str(car[8]))
mileage.insert(0,str(car[9]))
This is what i fixed:
stockNumber.get() returns a string, you compare it with an integer, if you do that, it will always be false -> convert it to int with int()
.insert
needs an index aswell, not just insert(data)
but insert(index, data)
Here is a hint!!!
You need to take a look at each point of data and understand the kind of data you are wanting your function to pull. Yes, you want a number, But is it an "int", a float? what is it???
Take a look at your function for search(). The only reason why it is not grabbing the data is that you need to make sure the function is looking for the right kind of data. Since "stock number" is probably an integer, you would need to specify that in your function.
Get a basic understanding of Python data types:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.