如何使用 python 中的 MySql 数据创建交互式列表
How can I create an interactive list using MySql data in python
我在我的 python 代码中使用 Tkinter,而不是显示站点中可用房间列表的代码,它显示一个列表,该列表使用 Entry
导致打印列表被更改而不是保持不变。有没有办法我可以编辑代码,以便它可以打印出我可以 select 为那个房间打开问题的按钮列表。
sitename3_info = sitename.get()
# the label should print all of the rooms in the site that was inputted in the audit function
cursor = cnn.cursor()
# retrieves the siteID from the inputted site name
siteID_fetch2 = "SELECT siteID FROM Sites WHERE siteName = %s"
cursor.execute(siteID_fetch2, [sitename3_info])
siteID_fetch2 = cursor.fetchall()
print(siteID_fetch2[0][0])
# searches for all the rooms for the site that was inputted
room = "SELECT roomname FROM rooms WHERE siteID_fk2 = %s"
cursor.execute(room,[siteID_fetch2[0][0]])
printrooms = cursor.fetchall()
print(printrooms[0][0])
# prints out a list of rooms in the site
i=0
for rooms in printrooms:
for j in range(len(rooms)):
e = Entry(screen13, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, rooms[j])
i=i+1
def change():
global pos
if j.get()==0:
b2['state']='NORMAL'
pos=0
elif j.get()==1:
b2['state']='NORMAL'
pos=1
elif j.get()==2:
b2['state']='NORMAL'
pos=2
elif j.get()==3:
b2['state']='NORMAL'
pos=3
elif j.get()==4:
b2['state']='NORMAL'
pos=4
def dele():
sql="""DELETE FROM myteam where Name = %s"""
if pos==0:
mycur.execute(sql,(name[0],))
if pos==1:
mycur.execute(sql,(name[1],))
if pos==2:
mycur.execute(sql,(name[2],))
if pos==3:
mycur.execute(sql,(name[3],))
if pos==4:
mycur.execute(sql,(name[4],))
db.commit()
messagebox.showinfo("ALERT","This player has been removed,select a new player")
new.destroy()
for i in range(0,len(name)):
name[i]=name[i].strip("\'")
for i in index:
i = i.strip('\"')
imgs.append(ImageTk.PhotoImage(Image.open(i)))
j=IntVar()
for i in range(0,len(index)):
l2.append(Radiobutton(new,variable=j,value=i,image=imgs[i],state=ACTIVE,command = change))
这是我用来 select 图像并使用单选按钮功能从 table 中删除该行的代码的一部分,您可以将删除功能替换为您选择执行的任何操作使用 selected.
按钮
您可以创建按钮而不是 Entry
小部件:
def action(room):
print(room)
# do whatever you want on the room
# assume OP code is inside a function
def search():
sitename3_info = sitename.get().strip()
if sitename3_info:
with cnn.cursor() as 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()
更新:不使用上下文管理器的示例:
def search():
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()
我在我的 python 代码中使用 Tkinter,而不是显示站点中可用房间列表的代码,它显示一个列表,该列表使用 Entry
导致打印列表被更改而不是保持不变。有没有办法我可以编辑代码,以便它可以打印出我可以 select 为那个房间打开问题的按钮列表。
sitename3_info = sitename.get()
# the label should print all of the rooms in the site that was inputted in the audit function
cursor = cnn.cursor()
# retrieves the siteID from the inputted site name
siteID_fetch2 = "SELECT siteID FROM Sites WHERE siteName = %s"
cursor.execute(siteID_fetch2, [sitename3_info])
siteID_fetch2 = cursor.fetchall()
print(siteID_fetch2[0][0])
# searches for all the rooms for the site that was inputted
room = "SELECT roomname FROM rooms WHERE siteID_fk2 = %s"
cursor.execute(room,[siteID_fetch2[0][0]])
printrooms = cursor.fetchall()
print(printrooms[0][0])
# prints out a list of rooms in the site
i=0
for rooms in printrooms:
for j in range(len(rooms)):
e = Entry(screen13, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, rooms[j])
i=i+1
def change():
global pos
if j.get()==0:
b2['state']='NORMAL'
pos=0
elif j.get()==1:
b2['state']='NORMAL'
pos=1
elif j.get()==2:
b2['state']='NORMAL'
pos=2
elif j.get()==3:
b2['state']='NORMAL'
pos=3
elif j.get()==4:
b2['state']='NORMAL'
pos=4
def dele():
sql="""DELETE FROM myteam where Name = %s"""
if pos==0:
mycur.execute(sql,(name[0],))
if pos==1:
mycur.execute(sql,(name[1],))
if pos==2:
mycur.execute(sql,(name[2],))
if pos==3:
mycur.execute(sql,(name[3],))
if pos==4:
mycur.execute(sql,(name[4],))
db.commit()
messagebox.showinfo("ALERT","This player has been removed,select a new player")
new.destroy()
for i in range(0,len(name)):
name[i]=name[i].strip("\'")
for i in index:
i = i.strip('\"')
imgs.append(ImageTk.PhotoImage(Image.open(i)))
j=IntVar()
for i in range(0,len(index)):
l2.append(Radiobutton(new,variable=j,value=i,image=imgs[i],state=ACTIVE,command = change))
这是我用来 select 图像并使用单选按钮功能从 table 中删除该行的代码的一部分,您可以将删除功能替换为您选择执行的任何操作使用 selected.
按钮您可以创建按钮而不是 Entry
小部件:
def action(room):
print(room)
# do whatever you want on the room
# assume OP code is inside a function
def search():
sitename3_info = sitename.get().strip()
if sitename3_info:
with cnn.cursor() as 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()
更新:不使用上下文管理器的示例:
def search():
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()