I am fairly new to Python and tkinter and the part I am struggling with now is getting additional values from a MySQL database once I select a business from a listbox.
The listbox will only list the business names, business id or username but when I select on the business, I have some labels that I need to automatically update from the database which do not appear in the listbox.
The labels that need to be updated are: First Name Username Password
The closest I can get is with the code below which updates the 'Username' label with the business name. I have tried so many ways to try and get the relevant value using indexes but can't get it to work.
# Select business from listbox
def select_business(event):
selection = event.widget.curselection()
#our_businesses.get(ANCHOR)
index = selection[0]
print('event:', event)
print('widget:', event.widget)
print('(event) current:', event.widget.get(event.widget.curselection()))
username_display_label.config(text=our_businesses.get(our_businesses.curselection()))
print('---')
# Search businesses
def search_now():
selected = drop.get()
sql = ""
if selected == "Search by...":
searched_label.config(text="You forgot to pick an option!")
if selected == "Business Name":
sql = """SELECT * FROM Retailers WHERE Retailer_Name like ? ORDER BY Retailer_Name ASC"""
searched_label.config(text="Businesses found:")
if selected == "Business ID":
sql = """SELECT * FROM Retailers WHERE Retailer like ? ORDER BY Retailer_Name ASC"""
searched_label.config(text="Businesses found:")
if selected == "Username":
sql = """SELECT * FROM Retailers WHERE Account_ID like ? ORDER BY Retailer_Name ASC"""
searched_label.config(text="Businesses found:")
searched = search_box.get()
#sql = "SELECT TOP 10 Retailer, Retailer_Name, Account_ID, Password FROM Retailers WHERE Retailer_Name = ?"
name = (f'%{searched}%', )
global businesses
businesses = c.execute(sql, name)
businesses = c.fetchall()
#Clear the listbox
our_businesses.delete(0, END)
if not businesses:
searched_label.config(text="Business not found")
else:
#global business
for business in businesses:
totals = len(businesses)
#print(totals)
our_businesses.insert(END, str(business[1]))
searched_label.config(text="Businesses found: " + str(totals))
Any help would be much appreciated!
print()to see which part is executed and what you have in variables. It is called"print debuging".select_business()you should run SQL code to get data from database - but you don't do this. Or if you have all information inbusinessesthen you should get frombusinesses- maybebusinesses[index]