0

I have some python code which generates buttons using the data in my SQL table. Is there a way I can get the text of the button I have selected, as the code I have will only retrieve the last value in the for loop. I can not make a variable for each button as each site will have a different number of rooms.

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):
            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()
        

Thank you!

Julia
  • 69
  • 8

1 Answers1

0

Edit: You would need to use a class to store an instance of each button.

use .cget()

from tkinter import *

screen13 = Tk()
screen13.geometry("300x250")
screen13.title("Rooms")

class buttongen():
    def __init__(self,x):
        self.btn = Button(screen13, text=word, command=lambda:print(self.btn.cget('text')))
        self.btn.pack()

for word in ('red', 'blue', 'orange', 'white'):
   buttongen(word)


screen13.mainloop()

I don't have access to your table but I tried to do it with your code it should work

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)

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()
coderoftheday
  • 1,987
  • 4
  • 7
  • 21
  • The buttons are automatically generated from what is stored in the database. I am wondering how I can get the text from a button that I have clicked as the code is getting the text from the last button that has been generated. – Julia Jan 21 '21 at 11:04
  • so the function in lambda should print the text of the button? – coderoftheday Jan 21 '21 at 11:08
  • how would I print the text on the button clicked – Julia Jan 28 '21 at 09:36
  • Nevermind I found where the issue was, where button(i,row) is the values below are unnecessary as the button is created twice. I removed the values below and kept the class, thank you – Julia Jan 28 '21 at 11:06