0

I'm trying make a function that creates new buttons according to the sql database:

search_room function returns the name of the professor and the room number of that office along with other information of the professor. I can search the name by typing the first two or three letters of the professor's name.

from tkinter import *
import pymysql

profname = Tk()
profname.title("PROF NAME")
profname.geometry("1280x800+0+0")
profname.config(bg='grey')

letters = Text(profname, width=21, height=1, font="times 30")
letters.place(x=350, y=200)

def search_room():

    conn = pymysql.connect(host='localhost', user='test', password='310', db='cdic', charset='utf8') 
    cursor = conn.cursor() 

    one_consonant = "SELECT * FROM profess_room where c1 = %s"
    two_consonant = "SELECT * FROM profess_room where c1 = %s and c2 = %s" 
    three_consonant = "SELECT * FROM profess_room where c1 = %s and c2 = %s and c3 = %s" 

    enter_input = letters.get('1.0','end')
    cursor.execute(two_consonant, (enter_input[0],enter_input[1])) 
    caseof_c1c2 = cursor.fetchall()
    cursor.execute(three_consonant, (enter_input[0],enter_input[1],enter_input[2]))
    caseof_c1c2c3 = cursor.fetchall()

    if len(enter_input)<4 and len(enter_input)>=3:
        num_of_caseof_c1c2=len(caseof_c1c2)
        i=0
        while num_of_caseof_c1c2>0 or i==0:
            line = Button(profname, width=100, height=2, text=(str(caseof_c1c2c3[i][0]) + 
            str(caseof_c1c2c3[i][1]) + str(caseof_c1c2c3[i][2]) + '\t\t\t' + str(caseof_c1c2c3[i][3]) + '\n'))
            line.configure(command=str(caseof_c1c2c3[i][0]))
            line.place(x=80, y=325+30*i)
            line.configure(font="times 17")
            num_of_caseof_c1c2 = num_of_caseof_c1c2-1
            i=i+1    
    elif len(enter_input)<5 and len(enter_input)>=4:
        num_of_caseof_c1c2c3=len(caseof_c1c2c3)
        i=0
        while num_of_caseof_c1c2c3>0 or i==0:
            line = Button(profname, width=70, height=2, text=(str(caseof_c1c2c3[i][0]) +
            str(caseof_c1c2c3[i][1]) + str(caseof_c1c2c3[i][2]) + '\t\t\t' + str(caseof_c1c2c3[i][3]) + '\n'))
            line.place(x=80, y=325+30*i)
            line.configure(font="times 17")
            num_of_caseof_c1c2c3 = num_of_caseof_c1c2c3-1
            i=i+1

execute_search = Button(profname, padx=2, pady=6, text = 'search', font='times 16', command=search_room)
execute_search.place(x=780, y=200)

profname.mainloop()

By pressing execute_searchbutton, I created each button line for every professor's name I get but keep failing when it comes to assigning the proper command to each button that pops up the final guidance route image that I have (which is a seperate function that I have).

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 2
    Does this answer your question? [tkinter creating buttons in for loop passing command arguments](https://stackoverflow.com/questions/10865116/tkinter-creating-buttons-in-for-loop-passing-command-arguments) – Thingamabobs Oct 10 '21 at 07:36
  • I think the problem that we're facing right now is pretty much what that link provides. Thank you for the notice, I'll look into that answer and work my way out :D – Sangmin Kim Oct 10 '21 at 08:27

0 Answers0