小二毛 发表于 2021-7-25 11:43:42

python连接MySQL如何实现修改功能

各位大佬帮我看一下,我想实现用户输入正确的用户名和密码,然后输入新的用户名和新的密码,就能修改成功,我现在的主要问题是,在def changepwd_ver():里面的功能实我不知道该怎么写了,{:7_119:} ,麻烦大家了,拜托拜托了{:7_119:}


import pymysql
from tkinter import *

def register():
    global register_screen
    register_screen = Toplevel(main_screen)
    register_screen.title("注册")
    register_screen.geometry("300x250")
    global username
    global password
    global username_entry
    global password_entryw
    username = StringVar()
    password = StringVar()
    Label(register_screen, text="请输入", bg="white").pack()
    Label(register_screen, text="").pack()
    username_lable = Label(register_screen, text="用户名 * ")
    username_lable.pack()
    username_entry = Entry(register_screen, textvariable=username)   
    username_entry.pack()
    password_lable = Label(register_screen, text="密码 * ")
    password_lable.pack()
    password_entry = Entry(register_screen, textvariable=password, show='*')
    password_entry.pack()
    Label(register_screen, text="").pack()
    Button(register_screen, text="注册", width=10, height=1, bg="white", command=register_user).pack()
   
def login():
    global login_screen
    login_screen = Toplevel(main_screen)
    login_screen.title("登录")
    login_screen.geometry("300x250")
    Label(login_screen, text="请输入").pack()
    Label(login_screen, text="").pack()
    global username_verify
    global password_verify
    username_verify = StringVar()
    password_verify = StringVar()
    global username_login_entry
    global password_login_entry
    Label(login_screen, text="用户名 * ").pack()
    username_login_entry = Entry(login_screen, textvariable=username_verify)
    username_login_entry.pack()
    Label(login_screen, text="").pack()
    Label(login_screen, text="密码 * ").pack()
    password_login_entry = Entry(login_screen, textvariable=password_verify, show='*')
    password_login_entry.pack()
    Label(login_screen, text="").pack()
    Button(login_screen, text="登录", width=10, height=1, command=login_verify).pack()
   
def changepwd():
    global changepwd_screen
    changepwd_screen = Toplevel(main_screen)
    changepwd_screen.title("修改")
    changepwd_screen.geometry("500x400")
    Label(changepwd_screen, text="请输入").pack()
    Label(changepwd_screen, text="").pack()
    global username_ver
    global password_ver
    global newusername_ver
    global newpassword_ver
    username_ver = StringVar()
    password_ver = StringVar()
    newusername_ver = StringVar()
    newpassword_ver = StringVar()
    global username_changepwd_entry
    global password_changepwd_entry
    global newusername_changepwd_entry
    global newpassword_changepwd_entry
    Label(changepwd_screen, text="用户名 * ").pack()   
    username_changepwd_entry = Entry(changepwd_screen, textvariable=username_ver)
    username_changepwd_entry.pack()
    Label(changepwd_screen, text="").pack()
    Label(changepwd_screen, text="密码 * ").pack()
    password_changepwd_entry = Entry(changepwd_screen, textvariable=password_ver, show='*')
    password_changepwd_entry.pack()
    Label(changepwd_screen, text="").pack()
    Label(changepwd_screen, text="x用户名 * ").pack()
    newusername_changepwd_entry = Entry(changepwd_screen, textvariable=username_ver)
    newusername_changepwd_entry.pack()
    Label(changepwd_screen, text="").pack()
    Label(changepwd_screen, text="x密码 * ").pack()
    newpassword_changepwd_entry = Entry(changepwd_screen, textvariable=newpassword_ver, show='*')
    newpassword_changepwd_entry.pack()
    Label(changepwd_screen, text="").pack()
    Button(changepwd_screen, text="修改", width=10, height=1, command=changepwd_ver).pack()

def delete():
    global register_screen
    delete_screen = Toplevel(main_screen)
    delete_screen.title("注销")
    delete_screen.geometry("300x250")
    global username
    global password
    global username_entry
    global password_entry
    username = StringVar()
    password = StringVar()
    Label(delete_screen, text="请输入", bg="white").pack()
    Label(delete_screen, text="").pack()
    username_lable = Label(delete_screen, text="用户名 * ")
    username_lable.pack()
    username_entry = Entry(delete_screen, textvariable=username)
    username_entry.pack()
    password_lable = Label(delete_screen, text="密码 * ")
    password_lable.pack()
    password_entry = Entry(delete_screen, textvariable=password, show='*')
    password_entry.pack()
    Label(delete_screen, text="").pack()
    Button(delete_screen, text="注销", width=10, height=1, bg="white", command=delete_verify).pack()

def delete_verify():
    username1 = username_verify.get()
    password1 = password_verify.get()
    username_login_entry.delete(0, END)
    password_login_entry.delete(0, END)

def register_user():
    username_info = username.get()
    password_info = password.get()
    conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='abc', port=3306, charset='utf8')
    cur = conn.cursor()

    select_sql = "select 用户名 from 用户信息表 WHERE 用户名=%s"
    cur.execute(select_sql, )
    result = cur.fetchone()
    if result is None:
      sql = 'insert into 用户信息表(用户名,密码)' \
            'values("%s","%s")' % \
            (username_info, password_info)
      try:
            cur.execute(sql)
            conn.commit()
            cur.close()
            conn.close()
      except:
            conn.rollback()
      username_entry.delete(0, END)
      password_entry.delete(0, END)
      Label(register_screen, text="注册成功!", fg="green", font=("宋体", 11)).pack()
    else:
      is_been_registered()


def login_verify():
    username1 = username_verify.get()
    password1 = password_verify.get()
    username_login_entry.delete(0, END)
    password_login_entry.delete(0, END)
    conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='abc', port=3306, charset='utf8')
    cur = conn.cursor()
    select_sql = "select 密码 from 用户信息表 WHERE 用户名=%s"
    cur.execute(select_sql,)
    result = cur.fetchone()
    if result is None:
      user_not_found()
    else :
      #print(result)
      if result == password1:
            login_sucess()
      elif result != password1:
            password_not_recognised()

def changepwd_ver():
    username2 = username_ver.get()
    password2 = password_ver.get()
    username_changepwd_entry.delete(0, END)
    password_changepwd_entry.delete(0, END)
    conn = pymysql.connect(host='localhost', user='root', password='123456', db='abc', port=3306, charset='utf8')
    cur = conn.cursor()

    select_sql = "select 密码 from 用户信息表 WHERE 用户名=%s"
    cur.execute(select_sql, )
    result = cur.fetchone()
   

def login_sucess():
    global login_success_screen
    login_success_screen = Toplevel(login_screen)
    login_success_screen.title("恭喜")
    login_success_screen.geometry("150x100")
    Label(login_success_screen, text="登录成功!").pack()
    Button(login_success_screen, text="确定", command=delete_login_success).pack()

   

def password_not_recognised():
    global password_not_recog_screen
    password_not_recog_screen = Toplevel(login_screen)
    password_not_recog_screen.title("失败")
    password_not_recog_screen.geometry("150x100")
    Label(password_not_recog_screen, text="密码错误 ").pack()
    Button(password_not_recog_screen, text="确定", command=delete_password_not_recognised).pack()

def is_been_registered():
    global is_been_registered_screen
    is_been_registered_screen = Toplevel(register_screen)
    is_been_registered_screen.title("失败")
    is_been_registered_screen.geometry("150x100")
    Label(is_been_registered_screen, text="该用户名已注册").pack()
    Button(is_been_registered_screen, text="确定", command=delete_is_been_registered_screen).pack()

def user_not_found():
    global user_not_found_screen
    user_not_found_screen = Toplevel(login_scree)
    user_not_found_screen.title("失败")
    user_not_found_screen.geometry("150x100")
    Label(user_not_found_screen, text="该用户未注册").pack()
    Button(user_not_found_screen, text="确认", command=delete_user_not_found_screen).pack()

def delete_login_success():
    login_success_screen.destroy()

def delete_password_not_recognised():
    password_not_recog_screen.destroy()

def delete_user_not_found_screen():
    user_not_found_screen.destroy()

def delete_is_been_registered_screen():
    is_been_registered_screen.destroy()

def main_account_screen():
    global main_screen
    main_screen = Tk()
    main_screen.geometry("500x400")
    main_screen.title("登录界面")
    Label(text="请点击按钮进行操作", bg="white", width="300", height="2", font=("宋体", 13)).pack()
    Label(text="").pack()
    Button(text="登录", height="2", width="30", command=login).pack()
    Label(text="").pack()
    Button(text="注册", height="2", width="30", command=register).pack()
    Label(text="").pack()
    Button(text="修改", height="2", width="30", command=changepwd).pack()
    Label(text="").pack()
    Button(text="注销", height="2", width="30", command=delete).pack()
    main_screen.mainloop()
main_account_screen()

阿奇_o 发表于 2021-7-25 15:14:09

你要不 你先试着改成 面向对象的,这么多global,你不难受吗~

小二毛 发表于 2021-7-25 19:30:50

阿奇_o 发表于 2021-7-25 15:14
你要不 你先试着改成 面向对象的,这么多global,你不难受吗~

{:10_266:}
页: [1]
查看完整版本: python连接MySQL如何实现修改功能