从数据库查询应用、MongoDB数据库
从数据库查询数据到tkinter表格中
import pymysql,tkinter from tkinter import ttk def serMySql(): db = pymysql.connect('localhost', 'root', 'passwd', 'class') cursor = db.cursor() name = 'student' sql = 'select * from '+name try: cursor.execute(sql) reslist = cursor.fetchall() # for row in reslist: # print('%d--%s--%s--%s' % (row[0], row[1], row[3],row[4])) return reslist except: print('失败') # 如果提交失败,回到上一次的数据 db.rollback() cursor.close() db.close() # 创建主窗口 win = tkinter.Tk() win.title("数据表查询界面") #设置大小和位置 win.geometry("420x300+200+0") # 表格,show='headings'不让最左边的行出现 tree = ttk.Treeview(win,show='headings') tree.pack() # 定义列 tree["columns"] = ("序号", "姓名", "年龄", "籍贯") # 设置列,现在列还不显示,列宽度为100 anchor='center'文字居中 tree.column("序号", width=100,anchor='center') tree.column("姓名", width=100,anchor='center') tree.column("年龄", width=100,anchor='center') tree.column("籍贯", width=120,anchor='center') # 设置表头 tree.heading("序号", text="序号-id") tree.heading("姓名", text="姓名-name") tree.heading("年龄", text="年龄-age") tree.heading("籍贯", text="籍贯-native place") # 向表格添加数据方法 def search(): a = serMySql() for index, row in enumerate(a): tree.insert("", index, values=(row[0], row[1], row[3], row[4])) button = tkinter.Button(win,text="查询",command=search) button.pack() win.mainloop()
来源https://blog.csdn.net/qq_36171287/article/details/96151266
l