使用Python和MySQL创建图形化界面的数据库课程设计

进行操作的表

 

增加界面

import tkinter as tk
import pymysql

def frame():
    global root
    root = tk.Tk()
    root.title('录入窗口')
    v1 = tk.StringVar()
    v2 = tk.StringVar()
    v3 = tk.StringVar()

    # 姓名标签,位置在第0行第0列
    tk.Label(root, text='姓名:').grid(row=0, column=0)
    # 电话标签,位置在第1行第0列
    tk.Label(root, text='电话:').grid(row=1, column=0)
    # 学号标签,位置在第3行第0列
    tk.Label(root, text='学号:').grid(row=2, column=0)
    # 姓名输入框
    global input1
    input1 = tk.Entry(root, textvariable=v1)
    input1.grid(row=0, column=1, padx=10, pady=5)
    # 电话输入框
    global input2
    input2 = tk.Entry(root, textvariable=v2)
    input2.grid(row=1, column=1, padx=10, pady=5)
    #学号输入框
    global input3
    input3 = tk.Entry(root, textvariable=v3)
    input3.grid(row=2, column=1, padx=10, pady=5)
    # 登录按钮
    tk.Button(root, text='录入', width=10, command=auto_insert1).grid(row=3, column=0, sticky=tk.W, padx=10, pady=5)
    # 退出按钮
    tk.Button(root, text='取消', width=10, command=exit_login).grid(row=3, column=1, sticky=tk.E, padx=10, pady=5)
    root.mainloop()


def auto_insert1():
    db = pymysql.connect(host="localhost", user="root", password="123456", db="test", port=3306)
    tcursor = db.cursor()
    entry1 = input1.get()
    entry2 = input2.get()
    entry3 = input3.get()
    sql = 'insert into client_copy values(%s,%s,%s)'
    param =(entry1,entry2,entry3)
    tcursor.execute(sql,param)
    print("数据插入成功\n")
    db.commit()


def exit_login():
    root.destroy()
    pass

if __name__ == '__main__':
    frame()

 

删除界面

import tkinter as tk
import pymysql

def frame():
    global root
    root = tk.Tk()
    root.title('删除窗口')
    v1 = tk.StringVar()


    # 姓名标签,位置在第0行第0列
    tk.Label(root, text='姓名:').grid(row=0, column=0)
    # 姓名输入框
    global input1
    input1 = tk.Entry(root, textvariable=v1)
    input1.grid(row=0, column=1, padx=10, pady=5)
    # 登录按钮
    tk.Button(root, text='删除', width=10, command=auto_delete).grid(row=3, column=0, sticky=tk.W, padx=10, pady=5)
    # 退出按钮
    tk.Button(root, text='取消', width=10, command=exit_login).grid(row=3, column=1, sticky=tk.E, padx=10, pady=5)
    root.mainloop()


def auto_delete():
    db = pymysql.connect(host="localhost", user="root", password="123456", db="test", port=3306)
    tcursor = db.cursor()
    entry1 = input1.get()
    sql = 'delete from client_copy where client_Name=%s'
    param =(entry1)
    tcursor.execute(sql,param)
    print("数据删除成功\n")
    db.commit()


def exit_login():
    root.destroy()
    pass

if __name__ == '__main__':
    frame()

 

修改界面

import tkinter as tk
import pymysql

def frame():
    global root
    root = tk.Tk()
    root.title('修改窗口')
    v1 = tk.StringVar()
    v2 = tk.StringVar()

    # 姓名标签,位置在第0行第0列
    tk.Label(root, text='新号码:').grid(row=0, column=0)
    # 电话标签,位置在第1行第0列
    tk.Label(root, text='学号为:').grid(row=1, column=0)

    # 姓名输入框
    global input1
    input1 = tk.Entry(root, textvariable=v1)
    input1.grid(row=0, column=1, padx=10, pady=5)
    # 电话输入框
    global input2
    input2 = tk.Entry(root, textvariable=v2)
    input2.grid(row=1, column=1, padx=10, pady=5)

    # 登录按钮
    tk.Button(root, text='修改', width=10, command=auto_update).grid(row=3, column=0, sticky=tk.W, padx=10, pady=5)
    # 退出按钮
    tk.Button(root, text='取消', width=10, command=exit_login).grid(row=3, column=1, sticky=tk.E, padx=10, pady=5)
    root.mainloop()


def auto_update():
    db = pymysql.connect(host="localhost", user="root", password="123456", db="test", port=3306)
    tcursor = db.cursor()
    entry1 = input1.get()
    entry2 = input2.get()
    sql = 'update client_copy set telephone=%s where studentNo=%s'
    param = (entry1, entry2)
    tcursor.execute(sql, param)
    print("数据修改成功\n")
    db.commit()

def exit_login():
    root.destroy()
    pass

if __name__ == '__main__':
    frame()

 

 查找界面

import tkinter as tk
import pymysql
# 导入消息对话框子模块
import tkinter.messagebox
global root,name,telephone,studentNo
global label
#import urllib
#创建主窗口


#查询按钮响应函数
def select(label,name,telephone,studentNo):
    sname = label.get()
    print('input: ',sname)
#查询刚才插入的数据
#由于刚才已经关闭了数据库连接,需要重新创建Connection对象和Cursor对象
    db= pymysql.connect(host="localhost", user="root", password="123456", db="test", port=3306)
    tcursor = db.cursor()
    #c = conn.execute('''select * from footballers''')
    #c = conn.execute("select * from footballers where name like?",(sname,))
    print("select * from client_copy where client_Name like '%s"+sname+"%s'")
    sql = 'select * from client_copy where client_Name like  %s'
    tcursor.execute(sql,sname)
    #print(c) #<sqlite3.Cursor object at 0x00000000007E25E0>
    list_re = tcursor.fetchall()
    print('result: ', list_re) #[('艾克森', '15', 'ChOxM1xC0BiAe2D7AAAN-qiRteQ443.png')]
    if len(list_re) <= 0:
        tkinter.messagebox.showinfo('提示',sname+'客户不存在,请输入其他客户姓名!')
    else:
        print('result_name: ', list_re[0][0])
    name.set(list_re[0][0])  # 姓 名
    telephone.set(list_re[0][1])  # 俱乐部
    studentNo.set(list_re[0][2])  # 国籍
    #数据成功提取出来了
    #name text, club text, nation text, height text, position text, age text, weight text, num text, birthday text, habit text
    db.close()
#定义一个返回按钮调用的返回函数:callback
def exit_program():
    quit()


def main():
    root = tk.Tk()
    root.title('客户姓名')
    # 设置窗口大小
    root.minsize(500, 200)
    # 定义变量
    name = tk.StringVar()
    name.set('')
    telephone = tk.StringVar()
    telephone.set('')
    studentNo = tk.StringVar()
    studentNo.set('')
    # name text, club text, nation text, height text, position text, age text, weight text, num text, birthday text, habit text
    input_name = tk.Label(root, text = '请输入客户姓名:').place(x = 30, y = 30)
    label = tk.StringVar()
    entry = tk.Entry(root,bg='#ffffff',width=20,textvariable=label).place(x=130,y=30,anchor='nw')
#按钮

    select_button = tk.Button(root,bg='white',text='查询',width=10,height=1,command=lambda :select(label,name,telephone,studentNo)).place(x=280,y=26,anchor='nw')
    exit_button = tk.Button(root,bg='white',text='退出',width=10,height=1,command=lambda :exit_program()).place(x=380,y=26,anchor='nw')
    #command是Button中的option项,可以指定点击button时调用的callback函数
    #name text, club text, nation text, height text, position text, age text, weight text, num text, birthday text, habit text
    le_client_Name = tk.Label(root, text = '姓 名:').place(x = 40, y = 80)
    le_student = tk.Label(root, text = '电 话:').place(x = 40, y = 110)
    le_studentNo = tk.Label(root, text = '学 号:').place(x = 40, y = 140)


    le_client_Name = tk.Label(root, textvariable=name).place(x=100, y=80)  # 姓 名
    le_telephone = tk.Label(root, textvariable=telephone).place(x=100, y=110)  # 电话
    le_studentNo = tk.Label(root, textvariable=studentNo).place(x=100, y=140)  # 学号
    #显示图片
    #pilImage = Image.open("imgs/1574777943.3190248.png")
    #tkImage = ImageTk.PhotoImage(image=pilImage)
    #label_nation = Label(root, image=tkImage).place(x=90, y=130, anchor='nw')
    root.mainloop()


if __name__ == '__main__':
    main()

 

物联沃分享整理
物联沃-IOTWORD物联网 » 使用Python和MySQL创建图形化界面的数据库课程设计

发表评论