I have a piece of code in python which generates buttons dependant on the rooms in a list in my database. I would like the code to return the room name on the button that I have selected and use it to store data in my database for that button
class buttongen():
def __init__(self,i,row):
self.i = i
self.row = row
self.roomname = self.row[0]
roomclicked = self.roomname
self.btn = Button(screen13, text=self.roomname, command=lambda :print("self.roomname"))
self.btn.grid(row=i, column=0)
Here is the class for each button and below is the code which gets the list of room names and prints them out as buttons. Is there a way I can store the text and use either .cget() or get() to return the name of the room
def search():
global screen13
global btn
global roomclicked
screen13 = Tk()
screen13.geometry("300x250")
screen13.title("Rooms")
sitename3_info = sitename.get().strip()
if sitename3_info:
cursor = cnn.cursor()
# combine the two SQL statements into one
sql = ("SELECT roomname FROM rooms, Sites "
"WHERE rooms.siteID_fk2 = Sites.siteID AND siteName = %s")
cursor.execute(sql, [sitename3_info])
rooms = cursor.fetchall()
# remove previous result (assume screen13 contains only result)
for w in screen13.winfo_children():
w.destroy()
if rooms:
for i, row in enumerate(rooms):
buttongen(i, row)
roomname = row[0]
roomclicked = roomname
btn = Button(screen13, text=roomname, command=lambda room=roomname: action(room))
btn.grid(row=i, column=0)
else:
Label(screen13, text="No room found").grid()
EDIT
this is the block to create the audit
def createaudit():
sitename2_info = sitename.get()
print(sitename2_info)
name2_info = name2.get()
print(name2_info)
name3_info = name3.get()
print(name3_info)
# Sql code for writing the data that was written in the regsitering page.
cursor = cnn.cursor()
# the site query matches the inputted username with the corresponding userID and inserts the userID into userID_fk
siteIDQuery = "SELECT siteID FROM Sites WHERE siteName = %s"
cursor.execute(siteIDQuery, [sitename2_info])
siteID_fetch = cursor.fetchall()
print(siteID_fetch[0][0])
sitequery = "INSERT INTO `audit`(`siteID_fk`, `auditor1`, `auditor2`) VALUES (%s, %s, %s)"
sitequery_vals = (siteID_fetch[0][0], name2_info, name3_info)
cursor.execute(sitequery, sitequery_vals)
# prints how many rows were inserted to make sure values are put into the database
print(cursor.rowcount)
cnn.commit()
if siteID_fetch:
for i in siteID_fetch:
search()
break
else:
failed2()
this is the block to print out the rooms within the site that's going to be audited
def search():
screen13 = Tk()
screen13.geometry("300x250")
screen13.title("Rooms")
sitename3_info = sitename.get().strip()
if sitename3_info:
cursor = cnn.cursor()
# combine the two SQL statements into one
sql = ("SELECT roomname FROM rooms, Sites "
"WHERE rooms.siteID_fk2 = Sites.siteID AND siteName = %s")
cursor.execute(sql, [sitename3_info])
rooms = cursor.fetchall()
# remove previous result (assume screen13 contains only result)
for w in screen13.winfo_children():
w.destroy()
if rooms:
for i, row in enumerate(rooms):
roomname = row[0]
btn = Button(screen13, text=roomname, command=lambda room=roomname: action(room))
btn.grid(row=i, column=0)
else:
Label(screen13, text="No room found").grid()
This is the block of code where I wish to answer questions for the room and store the answers in my database for it
def action(roomname):
global screen15
screen15 = Tk()
screen15.geometry("300x250")
screen15.title("Rooms")
global q1
global q2
global q3
q1 = StringVar()
q2 = StringVar()
q3 = StringVar()
Label(screen15, text = "Please enter details below", bg = "LightSkyBlue1", width = "300", height = "2").pack()
Label(screen15, text = "").pack()
Label(screen15, text = "01. Commodes CLEANING").pack()
q1_entry = Entry(screen15, textvariable = q1)
q1_entry.pack()
Label(screen15, text = "02. commodes NURSING").pack()
q2_entry = Entry(screen15, textvariable = q2)
q2_entry.pack()
Label(screen15, text = "03. Bathroom Hoists CLEANING").pack()
q3_entry = Entry(screen15, textvariable = q3)
q3_entry.pack()
Button(screen15, text = "Create an Audit", width = "12", height = "1", command = storescore).pack()
def storescore():
roomname2_info = buttongen(self.roomname).cget()
print(roomname2_info)
sitenameInfo = sitename.get()
# sql to store answer values
cursor = cnn.cursor()
siteID_fetch4 = "SELECT siteID FROM Sites WHERE siteName = %s"
cursor.execute(siteID_fetch4, [sitenameInfo])
siteID_fetch4 = cursor.fetchall()
print(siteID_fetch4[0][0])
roomsFID = "SELECT roomID FROM rooms WHERE siteID_fk2 = %s AND roomname = %s"
cursor.execute(roomsFID, [(siteID_fetch4[0][0]), (roomname2_info)])
roomsFID = cursor.fetchall()
print(roomsFID[0][0])
question from:
https://stackoverflow.com/questions/65907126/how-can-i-find-out-what-button-has-been-selected