tkinter - 将特定命令分配给新创建的按钮
问题描述
我正在尝试创建一个根据 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
为我得到的每个教授的名字创建了每个按钮,但是在为每个按钮分配正确的命令以弹出我拥有的最终引导路线图像(这是我拥有的一个单独的功能)时一直失败。
解决方案
推荐阅读
- swagger-ui - Swagger UI 中的掩码输入(示例 - 密码)?
- c# - Microsoft Graph API 安全 .net 核心客户端 ID
- android - 自定义主题未应用于 android listview 上的第一个 SwitchCompat 项目
- c# - C# Dynamic LINQ Contains 使用数组仅计算第一个值
- github - 如何将 github 中的两个分支与 GraphQL 进行比较?
- android - 我刚刚更新了工作线程上的 UI 没有问题,我不知道为什么
- python - Python语法错误——投币机问题
- c++ - std::iterator 是否继承自某种 auto_ptr?
- c++ - 如何读取特定数量的字符
- testing - “[TestNG] 未找到测试。没有运行任何内容”消息