当前位置:网站首页>Tkinter GUI address book management system
Tkinter GUI address book management system
2022-07-25 18:07:00 【Heart see heart】
tkinter GUI Version address book management system
The experimental requirements
To design a GUI Address book of version , Connect data.db database ( The database is QQ Group file ), Read out the address book records in the database , Place a table on the window to display the address book information , Use Treeview Component implementation , Then display the name, etc. in the form 6 A field , And add ( You can't add someone's information repeatedly ) And delete records . Improving the function requires error handling and information prompt when adding records . The interface is as follows :
Tips : The following is the main body of this article , The following cases can be used for reference
Complete code
import sqlite3
import tkinter
import tkinter.ttk
import tkinter.messagebox
def doSql(sql):
''' Used to perform SQL sentence , In especial INSERT and DELETE sentence '''
with sqlite3.connect('data.db') as conn:
cur = conn.cursor()
cur.execute(sql)
conn.commit()
# establish tkinter Application window
root = tkinter.Tk()
# Set window size and position
root.geometry('500x500+400+300')
# It is not allowed to change the window size
root.resizable(False, False)
# Set the window title
root.title(' Address book management system ')
# Place a label component and a text box component for entering names on the window
lbName = tkinter.Label(root, text=' full name :')
lbName.place(x=10, y=10, width=40, height=20)
entryName = tkinter.Entry(root)
entryName.place(x=60, y=10, width=150, height=20)
# Place the label component and combo box component for selecting gender on the window
lbSex = tkinter.Label(root, text=' Gender :')
lbSex.place(x=220, y=10, width=40, height=20)
comboSex = tkinter.ttk.Combobox(root,
values=(' male ', ' Woman '))
comboSex.place(x=270, y=10, width=150, height=20)
# Place a label component and a text box component for entering age on the window
lbAge = tkinter.Label(root, text=' Age :')
lbAge.place(x=10, y=50, width=40, height=20)
entryAge = tkinter.Entry(root)
entryAge.place(x=60, y=50, width=150, height=20)
# Place a label component and a text box component for entering departments on the window
lbDepartment = tkinter.Label(root, text=' department :')
lbDepartment.place(x=220, y=50, width=40, height=20)
entryDepartment = tkinter.Entry(root)
entryDepartment.place(x=270, y=50, width=150, height=20)
# Place a label component and a text box component for entering a phone number on the window
lbTelephone = tkinter.Label(root, text=' Telephone :')
lbTelephone.place(x=10, y=90, width=40, height=20)
entryTelephone = tkinter.Entry(root)
entryTelephone.place(x=60, y=90, width=150, height=20)
# Place the label component on the window and use it for input QQ Text box component of number
lbQQ = tkinter.Label(root, text='QQ:')
lbQQ.place(x=220, y=90, width=40, height=20)
entryQQ = tkinter.Entry(root)
entryQQ.place(x=270, y=90, width=150, height=20)
# Place a table on the window to display the address book information , Use Treeview Component implementation
frame = tkinter.Frame(root)
frame.place(x=0, y=180, width=480, height=280)
# Scroll bar
scrollBar = tkinter.Scrollbar(frame)
scrollBar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
#Treeview Components
treeAddressList = tkinter.ttk.Treeview(frame,
columns=('c1', 'c2', 'c3','c4', 'c5', 'c6'),
show="headings",
yscrollcommand = scrollBar.set)
treeAddressList.column('c1', width=70, anchor='center')
treeAddressList.column('c2', width=50, anchor='center')
treeAddressList.column('c3', width=50, anchor='center')
treeAddressList.column('c4', width=110, anchor='center')
treeAddressList.column('c5', width=100, anchor='center')
treeAddressList.column('c6', width=100, anchor='center')
treeAddressList.heading('c1', text=' full name ')
treeAddressList.heading('c2', text=' Gender ')
treeAddressList.heading('c3', text=' Age ')
treeAddressList.heading('c4', text=' department ')
treeAddressList.heading('c5', text=' Telephone ')
treeAddressList.heading('c6', text='QQ')
treeAddressList.pack(side=tkinter.LEFT, fill=tkinter.Y)
# Treeview Component combined with vertical scroll bar
scrollBar.config(command=treeAddressList.yview)
# Definition Treeview Left click event of component , And bind to Treeview Components
# Click the left mouse button , Set a variable nameToDelete Value , And then you can use “ Delete ” Button to delete
nameToDelete = tkinter.StringVar('')
def treeviewClick(event):
if not treeAddressList.selection():
return
item = treeAddressList.selection()[0]
nameToDelete.set(treeAddressList.item(item, 'values')[0])
treeAddressList.bind('<Button-1>', treeviewClick)
def bindData():
''' Read out the address book records in the database , Then display in the table '''
# Delete all the original rows in the table
for row in treeAddressList.get_children():
treeAddressList.delete(row)
# Read all data in the database
with sqlite3.connect('data.db') as conn:
cur = conn.cursor()
cur.execute('select * from addressList order by id')
temp = cur.fetchall()
# Insert the data into the table
for i, item in enumerate(temp):
treeAddressList.insert('', i, values=item[1:])
# Call function , Display the records in the database in the table
bindData()
# Place a button on the window to add the address book , And set the button click event function
def buttonAddClick():
# Check name
name = entryName.get().strip()
if name == '':
tkinter.messagebox.showerror(title=' I'm sorry ', message=' Name cannot be empty !')
return
# The name cannot be repeated
with sqlite3.connect('data.db') as conn:
cur = conn.cursor()
cur.execute('select COUNT(id) FROM addressList WHERE name="{}"'.format(name))
c = cur.fetchone()[0]
if c!=0:
tkinter.messagebox.showerror(title=' I'm sorry ', message=' The name cannot be repeated !')
return
# Get the selected gender
sex = comboSex.get()
if sex not in (' male ', ' Woman '):
tkinter.messagebox.showerror(title=' I'm sorry ', message=' Gender cannot be empty !')
return
# Age of examination
age = entryAge.get().strip()
if age == '':
tkinter.messagebox.showerror(title=' I'm sorry ', message=' Age cannot be empty !')
return
# Inspection department
department = entryDepartment.get().strip()
if department == '':
tkinter.messagebox.showerror(title=' I'm sorry ', message=' Department cannot be empty !')
return
# Check the phone number
telephone = entryTelephone.get().strip()
if telephone=='' or (not telephone.isdigit()):
tkinter.messagebox.showerror(title=' I'm sorry ', message=' The phone number is empty or wrong !')
return
# Check QQ number
qq = entryQQ.get().strip()
if qq=='' or (not qq.isdigit()):
tkinter.messagebox.showerror(title=' I'm sorry ', message='QQ The number is empty or wrong !')
return
# All inputs passed the check , Insert database
sql = 'INSERT INTO addressList(name,sex,age,department,telephone,qq) VALUES("'\
+ name + '","' + sex + '",' + str(age) + ',"' + department + '","'\
+ telephone + '","' + qq + '")'
doSql(sql)
# After adding records , Update the data in the table
bindData()
buttonAdd = tkinter.Button(root, text=' add to ', command=buttonAddClick)
buttonAdd.place(x=100, y=140, width=80, height=20)
# Place a button on the window to delete the address book , And set the button click event function
def buttonDeleteClick():
name = nameToDelete.get()
if name == '':
tkinter.messagebox.showerror(title=' I'm sorry ', message=' No record selected !')
return
# If you have selected an address book , perform SQL Statement to delete
sql = 'DELETE FROM addressList WHERE name="{}"'.format(name)
doSql(sql)
tkinter.messagebox.showinfo(' Congratulations ', ' Delete successful ')
# Reset the variable to an empty string
nameToDelete.set('')
# Update the data in the table
bindData()
buttonDelete = tkinter.Button(root, text=' Delete ', command=buttonDeleteClick)
buttonDelete.place(x=240, y=140, width=80, height=20)
root.mainloop()
Running results
Set parameters : full name : Wang Wu , Gender : male , Age :20, department : Finance Department , Telephone :666,QQ:888
experimental result : 
Set parameters : full name : Zhang San , Gender : male , Age :35, department : The personnel department , Telephone :1234567,QQ:101010
experimental result : 
Select a record , Delete them
experimental result : 
边栏推荐
- Auditing related notes
- Hit the test site directly: summary of common agile knowledge points in PMP examination
- Joseph Ring problem
- Error when starting MySQL on Linux
- SQL optimizer parsing | youth training camp notes
- OSPF --- open shortest priority path protocol
- Principle and implementation of UDP penetration NAT in P2P
- SLA 、SLO & SLI
- NPDP多少分通过?如何高分通过?
- 简述冒泡排序与快速排序
猜你喜欢

"Digital security" alert NFT's seven Scams

Drawing PDF form (II) drawing excel form style in PDF through iText, setting Chinese font, watermark, logo, header and page number

Cloud XR面临的问题以及Cloud XR主要应用场景

Wu Enda's machine learning programming operation cannot be suspended pause problem solved

如何判断静态代码质量分析工具的性能?这五大因素必须考虑

Installation and operation instructions of SVN client (TortoiseSVN)

How to choose digital twin visualization platform

Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution

「数字安全」警惕 NFT的七大骗局

Kendryte K210 在freertos上的lcd屏幕的使用
随机推荐
H5测试点(思维导图)
Go language context control function execution timeout return
Basic knowledge of software testing (mind mapping)
IDEA集成SVN代码管理常用功能
C language libcurl cross compilation
STM32F105RBT6 内部flash调试
ORB_SLAM3复现——上篇
3DCAT v2.1.3新版本发布,这三大功能更新你不容错过!
图的相关操作
Could not stop Cortex-M device! Please check the JTAG cable solution
OV7725 yuv 640*[email protected] 配置文件
直击考点:PMP考试中常见敏捷知识点汇总
简述Synchronized以及锁升级
MySQL page lock
MySQL lost the previous 0 after the decimal number type select
Connect to MySQL using sqldeveloper
Go defer and recover simple notes
C语言 cJSON库的使用
Mock服务moco系列(一)- 简介、第一个Demo、Get请求、Post请求
Oracle导入出错:IMP-00038: 无法转换为环境字符集句柄