I'm just trying to get to grips with some basic Python (on GTK) after having not programmed in years. I'm putting together a small application that searches a user given string in a database and returns the search result. I get as far as sending the search string down to a function, pulling the result from the DB and assigning it to a string. However, I'm not sure how I go about retrieving the result from the main part of the program and updating a widget with the returned text.
This part of the main code sends the search result to one of two functions depending on if Enter is hit or the search icon is clicked.
#Add Search box
entry = Gtk.Entry()
entry.set_property("secondary-icon-stock", Gtk.STOCK_FIND)
entry.connect("icon-press", self.on_search_button)
entry.connect("activate", self.on_search_enter)
The search function takes in the search text and calls the lookup function.
def on_search_enter(self, entry):
search = entry.get_text()
entry.set_text("")
print("Search for " + search)
result = self.lookup_player(search, entry)
print result
So here I'm trying to pass the string, the entry box object and get back the result, a string called
playername which I (temporarily) display on a label which is displayed in a table, both of which are part of the main body, as is the string.
def lookup_player(self, pstring, entry):
try:
con = psycopg2.connect(database='Rugby', user='postgres', password = '1234')
cur = con.cursor()
search = "select firstname, lastname from player where firstname ilike '" + pstring + "' or lastname ilike '" + pstring + "'"
print search
cur.execute(search)
ver = cur.fetchall()
entry.playername = ver
print ver
So, that's confusing to read...
Basically, how to get that string read from the database back to the main body and force an existing label to update with the returned string.