一、设置主窗口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# -*- coding: utf-8 -*- import tkinter from tkinter import ttk import pymysql # 导入消息对话框子模块 import tkinter.messagebox # 创建主窗口 root = tkinter.Tk() root.title( '告警查询' ) # 设置窗口大小 root.minsize( 500 , 500 ) tabControl = ttk.Notebook(root) tab1 = ttk.Frame(tabControl) # Create a tab tabControl.add(tab1, text = '铁塔订单查询' ) # Add the tab tab2 = ttk.Frame(tabControl) # Add a second tab tabControl.add(tab2, text = '告警详细查询' ) # Make second tab visible tab3 = ttk.Frame(tabControl) # Add a third tab tabControl.add(tab3, text = '配置汇总表' ) # Make second tab visible tabControl.pack(expand = 1 , fill = "both" ) |
二、定义函数
1.定义增删改查函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
'''main3响应函数''' def select3(root, label,tree): sname = label.get() print ( 'input: ' ,sname) # 1.由于刚才已经关闭了数据库连接,需要重新创建Connection对象和Cursor对象 con = pymysql.connect(host = '192.168.0.46' , user = 'root' , password = 'root' , database = 'test' , charset = 'utf8' , use_unicode = True ) cursor = con.cursor() print ( "select * from 配置表汇总 where `站点名称(局向)` like '" + sname + "'") c = cursor.execute("select * from 配置表汇总 where `站点名称(局向)` like '"+sname+"'" ) # 2.查询结果保存到list_re list_re = cursor.fetchall() print ( 'result: ' , list_re) if len (list_re) < = 0 : tkinter.messagebox.showinfo( '提示' ,sname + '告警信息不存才!' ) else : print ( 'result_name: ' , list_re[ 0 ][ 0 ]) #数据成功提取出来了 # 3.向tree写入数据 for i in range ( len (list_re)): tree.insert('', i, text = i ,values = (list_re[i][ 0 ], list_re[i][ 2 ],list_re[i][ 4 ], list_re[i][ 14 ],\ list_re[i][ 5 ], list_re[i][ 9 ],list_re[i][ 11 ], list_re[i][ 12 ],\ list_re[i][ 13 ])) tree.grid(column = 0 ,row = 1 ,sticky = 'NSEW' ) con.close() def insert(root, label1,label2,label3,label4,label5,label6,label7,label8,label9): net_name = label1.get() area_name = label2.get() base_name = label3.get() belong_name = label4.get() base_local = label5.get() base_level = label6.get() base_num = label7.get() rural = label8.get() scene = label9.get() print ( 'input: ' ,net_name) # 由于刚才已经关闭了数据库连接,需要重新创建Connection对象和Cursor对象 con = pymysql.connect(host = '192.168.0.46' , user = 'root' , password = 'root' , database = 'test' , charset = 'utf8' , use_unicode = True ) cursor = con.cursor() # SQL 插入语句 里面的数据类型要对应 sql = "INSERT INTO 配置表汇总(`网络类型`,\ `行政区`,`基站名称`,`归属区域`,`基站所处位置具体地址`,\ `基站分级`,`站点名称(局向)`,`是否农村基站`,`二级场景类型`)\ VALUES ( '%s' , '%s' , '%s' , '%s' , '%s' , '%s' , '%s' , '%s' , '%s' )" % \ (net_name,area_name,base_name,belong_name,base_local,base_level,base_num,rural,scene) print (sql) try : # 执行sql语句 cursor.execute(sql) # 执行sql语句 con.commit() tkinter.messagebox.showinfo( '提示' ,net_name + '配置汇总表插入成功!' ) except : # 发生错误时回滚 con.rollback() con.close() def delete(root,label7): base_num = label7.get() print ( 'input: ' ,base_num) # 由于刚才已经关闭了数据库连接,需要重新创建Connection对象和Cursor对象 con = pymysql.connect(host = '192.168.0.46' , user = 'root' , password = 'root' , database = 'test' , charset = 'utf8' , use_unicode = True ) cursor = con.cursor() # SQL 插入语句 里面的数据类型要对应 sql = "DELETE FROM 配置表汇总 WHERE `站点名称(局向)`='%s'" % (base_num) print (sql) try : # 执行sql语句 cursor.execute(sql) # 执行sql语句 con.commit() tkinter.messagebox.showinfo( '提示' ,base_num + '配置汇总表删除成功!' ) except : # 发生错误时回滚 con.rollback() con.close() |
2.定义主调用函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
def main3(): monty3 = ttk.LabelFrame(tab3, text = '控件示范区3' ) monty3.grid(column = 0 , row = 0 ,sticky = 'W' , padx = 8 , pady = 4 ) monty3_1 = ttk.LabelFrame(tab3, text = '控件示范区2' ) monty3_1.grid(column = 0 , row = 1 ,sticky = 'W' , padx = 4 , pady = 4 ) # 查询结果 tree = ttk.Treeview(monty3_1) #表格 tree[ "columns" ] = ( "网络类型" , "行政区" , "基站名称" , "归属区域" , "基站所处位置具体地址" ,\ "基站分级" , "站点名称(局向)" , "是否农村基站" , "二级场景类型" ) tree.column( "网络类型" ,width = 50 ) #表示列,不显示 tree.column( "行政区" ,width = 50 ) tree.column( "基站名称" ,width = 50 ) tree.column( "归属区域" ,width = 50 ) #表示列,不显示 tree.column( "基站所处位置具体地址" ,width = 50 ) tree.column( "基站分级" ,width = 50 ) tree.column( "站点名称(局向)" ,width = 50 ) #表示列,不显示 tree.column( "是否农村基站" ,width = 50 ) tree.column( "二级场景类型" ,width = 50 ) tree.heading( "网络类型" ,text = "网络类型" ) #显示表头 tree.heading( "行政区" ,text = "行政区" ) tree.heading( "基站名称" ,text = "基站名称" ) tree.heading( "归属区域" ,text = "归属区域" ) #显示表头 tree.heading( "基站所处位置具体地址" ,text = "基站所处位置具体地址" ) tree.heading( "基站分级" ,text = "基站分级" ) tree.heading( "站点名称(局向)" ,text = "站点名称(局向)" ) #显示表头 tree.heading( "是否农村基站" ,text = "是否农村基站" ) tree.heading( "二级场景类型" ,text = "二级场景类型" ) tree.grid(column = 0 ,row = 1 ,sticky = 'NSEW' ) input_name1 = ttk.Label(monty3, text = '网络类型:' ).grid(column = 0 , row = 0 , sticky = 'W' ,pady = 5 ) label1 = tkinter.StringVar() entry1 = tkinter.Entry(monty3,bg = '#ffffff' ,width = 20 ,textvariable = label1).grid(column = 1 , row = 0 , sticky = 'W' ) input_name2 = ttk.Label(monty3, text = '行政区:' ).grid(column = 3 , row = 0 , sticky = 'W' ) label2 = tkinter.StringVar() entry2 = tkinter.Entry(monty3,bg = '#ffffff' ,width = 20 ,textvariable = label2).grid(column = 4 , row = 0 , sticky = 'W' ) input_name3 = ttk.Label(monty3, text = '基站名称:' ).grid(column = 0 , row = 1 , sticky = 'W' ,pady = 5 ) label3 = tkinter.StringVar() entry3 = tkinter.Entry(monty3,bg = '#ffffff' ,width = 20 ,textvariable = label3).grid(column = 1 , row = 1 , sticky = 'W' ) input_name4 = ttk.Label(monty3, text = '归属区域:' ).grid(column = 3 , row = 1 , sticky = 'W' ) label4 = tkinter.StringVar() entry4 = tkinter.Entry(monty3,bg = '#ffffff' ,width = 20 ,textvariable = label4).grid(column = 4 , row = 1 , sticky = 'W' ) input_name5 = ttk.Label(monty3, text = '基站所处位置具体地址:' ).grid(column = 0 , row = 2 , sticky = 'W' ,pady = 5 ) label5 = tkinter.StringVar() entry5 = tkinter.Entry(monty3,bg = '#ffffff' ,width = 20 ,textvariable = label5).grid(column = 1 , row = 2 , sticky = 'W' ) input_name6 = ttk.Label(monty3, text = '基站分级:' ).grid(column = 0 , row = 3 , sticky = 'W' ,pady = 5 ) label6 = tkinter.StringVar() entry6 = tkinter.Entry(monty3,bg = '#ffffff' ,width = 20 ,textvariable = label6).grid(column = 1 , row = 3 , sticky = 'W' ) input_name7 = ttk.Label(monty3, text = '站点名称(局向):' ).grid(column = 0 , row = 4 , sticky = 'W' ,pady = 5 ) label7 = tkinter.StringVar() entry7 = tkinter.Entry(monty3,bg = '#ffffff' ,width = 20 ,textvariable = label7).grid(column = 1 , row = 4 , sticky = 'W' ) input_name8 = ttk.Label(monty3, text = '是否农村基站:' ).grid(column = 0 , row = 5 , sticky = 'W' ,pady = 5 ) label8 = tkinter.StringVar() entry8 = tkinter.Entry(monty3,bg = '#ffffff' ,width = 20 ,textvariable = label8).grid(column = 1 , row = 5 , sticky = 'W' ) input_name9 = ttk.Label(monty3, text = '二级场景类型:' ).grid(column = 0 , row = 6 , sticky = 'W' ,pady = 5 ) label9 = tkinter.StringVar() entry9 = tkinter.Entry(monty3,bg = '#ffffff' ,width = 20 ,textvariable = label9).grid(column = 1 , row = 6 , sticky = 'W' ) select_button = tkinter.Button(monty3,bg = 'white' ,text = '查询' ,width = 10 ,height = 1 ,\ command = lambda :select3(monty3, label7,tree)).grid(column = 0 , row = 7 , sticky = 'W' ,pady = 5 ) insert_button = tkinter.Button(monty3,bg = 'white' ,text = '插入' ,width = 10 ,height = 1 ,\ command = lambda :insert(monty3,label1,label2,label3,label4,label5,label6,\ label7,label8,label9)).grid(column = 1 , row = 7 , sticky = 'W' ,padx = 5 ,pady = 5 ) delete_button = tkinter.Button(monty3,bg = 'white' ,text = '删除' ,width = 10 ,height = 1 ,\ command = lambda :delete(monty3, label7)).grid(column = 2 , row = 7 , sticky = 'W' ,pady = 5 ) |
效果如下
到此这篇关于Python中tkinter+MySQL实现增删改查的文章就介绍到这了,更多相关tkinter MySQL增删改查内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_35350265/article/details/105506728