问题遇到的现象和发生背景
我在做一个关于将sqlite和tkinter相结合的东西,我想实现这么一个功能,通过遍历数据库获取表名并创建按钮,但是遍历完后按钮为最后一次遍历的结果,所以我只能实现最后一个遍历的功能
问题相关代码,请勿粘贴截图
v = tkinter.IntVar(master=self.show_window)
def click():
add_content = Label(self.show_window, text='ff:')
add_content.place(relx=0.26,rely=0.1)
e_con = Entry(self.show_window)
e_con.place(relx=0.34,rely=0.1)
add_source = Label(self.show_window, text='dd:')
add_source.place(relx=0.26,rely=0.21)
e_sour = Entry(self.show_window)
e_sour.place(relx=0.34,rely=0.21)
def cancel():
if add_content and add_source and e_con and e_sour and sure_button and cancel_button:
add_content.destroy()
add_source.destroy()
e_con.destroy()
e_sour.destroy()
sure_button.destroy()
cancel_button.destroy()
sure_button = Button(self.show_window, text="确认", command=sure)
sure_button.place(relx=0.34,rely=0.32)
cancel_button = Button(self.show_window, text="取消", command=cancel)
cancel_button.place(relx=0.43,rely=0.32)
def sure():
for name in self.true_list:
print("diff名字", diff['text'])
if name == diff['text']:
new_con = e_con.get()
new_sour = e_sour.get()
print(new_con, new_sour)
conn = sqlite3.connect(r'sens.db')
c = conn.cursor()
c.execute(f"insert into '{name}'(id,content,source) values(null,'{new_con}','{new_sour}');")
conn.commit()
else:
continue
loca_num = 0.1
for name in self.true_list:
print(name)
diff = tkinter.Radiobutton(self.show_window, text=name, command=click,value=loca_num,variable=v)
# diff.grid(row=loca_num, column=3)
diff.place(relx=0.1,rely=loca_num)
print(diff)
loca_num += 0.1
self.show_window.mainloop()
运行结果及报错内容
可以运行,但是仅能识别最后一次遍历的
我的解答思路和尝试过的方法
我尝试在循环里面写方法,但最后方法也相应的只有在最后一次遍历才可使用
我希望可以在对应的按钮实现对应的功能