首页 > 解决方案 > 将特定命令分配给新创建的按钮

问题描述

我正在尝试创建一个根据 sql 数据库创建新按钮的函数:

search_room函数返回教授的姓名和该办公室的房间号以及教授的其他信息。我可以通过键入教授姓名的前两个或三个字母来搜索姓名。

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

通过按下execute_search按钮,我line为我得到的每个教授的名字创建了每个按钮,但是在为每个按钮分配正确的命令以弹出我拥有的最终引导路线图像(这是我拥有的一个单独的功能)时一直失败。

标签: tkinter

解决方案


推荐阅读