Reputation: 89
I tried to operate on tkinter entry ... The value of that entry is used as integer type in the database.. I converted my entry value to integer as follows...
self.yr=(int(self.year.get()))
self.isb=(int(self.isbn.get()))
eventhough it throws ValueError..
How to convert string value from an ENTRY WIDGET to an integer value to be used as an argument for my search function in Postgresql database..
My Backend search function is: here in the code..[year,isbn are integer values]
def search(self,title=None,author=None,year=None,isbn=None):
self.cur.execute("SELECT * FROM books WHERE title=%s or author=%s or year=%s or isbn=%s",(title,author,year,isbn))
row=self.cur.fetchall()
return row
My Frontend code is:
from tkinter import *
from books_db import database
db=database()
class Window(object):
def __init__(self,window):
self.window=window
self.window.wm_title("BOOK STORE")
############# ENTRIES.................
self.title=Entry(window)
self.title.grid(row=0,column=1)
self.year=Entry(window)
self.year.grid(row=1,column=1)
self.yr=(int(self.year.get()))
self.author=Entry(window)
self.author.grid(row=0,column=3)
self.isbn=Entry(window)
self.isbn.grid(row=1,column=3)
self.isb=(int(self.isbn.get()))
############ LISTBOX.................
self.list=Listbox(window,height=6,width=35)
self.list.grid(row=2,column=0,rowspan=6,columnspan=2)
########## BUTTONS...............
b2=Button(window,text="SEARCH ENTRY",width=15,command=self.search_cmd)
b2.grid(row=3,column=3)
def search_cmd(self):
self.list.delete(0,END)
for item in db.search(self.title.get(),self.author.get(),self.yr,self.isb):
self.list.insert(END,item)
window=Tk()
Window(window)
window.mainloop()
Upvotes: 0
Views: 4428
Reputation: 386325
Consider these three lines of code:
self.isbn=Entry(window)
self.isbn.grid(row=1,column=3)
self.isb=(int(self.isbn.get()))
You are creating a Entry
widget, and about a microsecond later you are getting the value and converting it to an int. The user will not have even seen the entry widget, much less typed into it. Thus, the value will be the empty string, and the call to int()
will fail.
Your code must wait to call get()
until after the user has had a chance to enter data.
Upvotes: 3