菜鸟试做GUI简单数据库查询界面 python+tkinter+mysql

一、准备工作:

1、安装mysql3.7,创建一个test数据库,创建student表,创建列:(列名看代码),创建几条数据

(以上工作直接用navicat for mysql工具完成)

二、代码:

1 import sys 2 import tkinter as tk 3 import mysql.connector as sql 4 5 #--------------------查询函数--------------------------- 6 def sql_connect(): 7 listbox_show.delete(0,'end') #初始化清空显示区 8 m = listbox_name.curselection()[0] 9 print('m=',m) 10 list_name = listbox_name.get(m)[0] 11 varlue = entry_varlue.get() 12 print('list_name=',list_name) 13 print('var=',varlue) 14 15 while list_name == 'Student_ID': 16 sql_sr = "select * from Student where student.student_id = " + "'" + varlue +"'" 17 print(sql_sr) 18 break 19 while list_name == 'Name': 20 sql_sr = "select * from Student where student.name = " + "'" + varlue +"'" 21 print(sql_sr) 22 break 23 24 while list_name == 'Age': 25 sql_sr = "select * from Student where student.age = " + "'" + varlue +"'" 26 print(sql_sr) 27 break 28 while list_name == 'Sex': 29 sql_sr = "select * from Student where student.sex = " + "'" + varlue +"'" 30 print(sql_sr) 31 break 32 while list_name == 'Class': 33 sql_sr = "select * from Student where student.class = " + "'" + varlue +"'" 34 print(sql_sr) 35 break 36 37 mydb = sql.connect(host='localhost',user='root',passwd='123456',database='test') 38 mycursor = mydb.cursor() 39 mycursor.execute(sql_sr) 40 myresult = mycursor.fetchall() 41 for x in myresult: 42 print (x) 43 listbox_show.insert('end',x) 44 #-------------------------------------------------------- 45 46 47 #------------界面初始化时加载选项列表-------------------- 48 sql_n = "SELECT COLUMN_NAME FROM information_schema.COLUMNS \ 49 WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME = 'student'" 50 mydb = sql.connect(host='localhost',user='root',passwd='123456',database='test') 51 mycursor = mydb.cursor() 52 mycursor.execute(sql_n) 53 myresult = mycursor.fetchall() 54 55 #-------------------------------------------------------- 56 57 #----------窗体部分-------------------------------------- 58 entry_list_name = tk.Entry() 59 entry_list_name.pack() 60 61 listbox_name = tk.Listbox() 62 listbox_name.pack() 63 for i in myresult: 64 listbox_name.insert('end',i) #加载选项列表 65 66 entry_varlue = tk.Entry() 67 entry_varlue.pack() 68 69 button_select = tk.Button(text = '查找',command = sql_connect) 70 button_select.pack() 71 72 listbox_show = tk.Listbox() 73 listbox_show.pack() 74 75 76 root.mainloop() 77 #-------------------------------------------------------- 78

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsxfdx.html