最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python+Tkinter簡單實現(xiàn)注冊登錄功能

 更新時間:2022年02月08日 11:50:12   作者:滄海黎明  
這篇文章主要為大家詳細介紹了Python+Tkinter簡單實現(xiàn)注冊登錄功能,連接本地MySQL數(shù)據(jù)庫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Python+Tkinter簡單實現(xiàn)注冊登錄功能的具體代碼,供大家參考,具體內(nèi)容如下

項目結構:

源代碼:

# -*- coding: utf-8 -*-
"""
@date: ?2022/01/09 17:40
@author: Anker
@python:v3.10
"""
?
import tkinter as tk
import tkinter.messagebox
import pymysql
?
# 定義要執(zhí)行的創(chuàng)建表的SQL語句
test_sql = """
? ? ? ? ? ? ? ? CREATE TABLE IF NOT EXISTS user(
? ? ? ? ? ? ? ? id INT auto_increment PRIMARY KEY,
? ? ? ? ? ? ? ? name varchar(20) not null,
? ? ? ? ? ? ? ? password varchar(20) not null
? ? ? ? ? ? ? ? )ENGINE=innodb DEFAULT CHARSET=utf8;
? ? ? ? ? ?"""
?
# 登錄窗口
window = tk.Tk()
window.title('學生考試系統(tǒng)')
window.geometry('800x500')
?
# 登錄背景圖片
canvas = tk.Canvas(window, height=1920, width=1080)
login_background = tk.PhotoImage(file='./view.png')
login_image = canvas.create_image(0, 0, anchor='nw', image=login_background)
canvas.pack(side='top')
?
# 用戶名密碼標簽
tk.Label(window, text='用戶名:', bg='yellow').place(x=300, y=200)
tk.Label(window, text='密 ? 碼:', bg='yellow').place(x=300, y=250)
?
# 用戶名輸入框
var_user_name = tk.StringVar()
entry_user_name = tk.Entry(window, textvariable=var_user_name)
entry_user_name.place(x=370, y=200)
?
# 密碼輸入框
var_user_pwd = tk.StringVar()
entry_user_pwd = tk.Entry(window, textvariable=var_user_pwd, show='*')
entry_user_pwd.place(x=370, y=250)
?
?
# 登錄函數(shù)
def user_login():
? ? # 輸入框獲取用戶名密碼
? ? user_name = var_user_name.get()
? ? user_password = var_user_pwd.get()
? ? # 連接test_sql數(shù)據(jù)庫
? ? conn = pymysql.connect(host="localhost", user="root", password="123456", database="test_sql", charset="utf8")
? ? curs = conn.cursor()
? ? # 執(zhí)行SQL語句,創(chuàng)建user數(shù)據(jù)表
? ? curs.execute(test_sql)
? ? # 執(zhí)行SQL語句,從user數(shù)據(jù)表中查詢name和password字段值
? ? curs.execute("SELECT name,password FROM user")
? ? # 將數(shù)據(jù)庫查詢的結果保存在result中
? ? result = curs.fetchall()
? ? # fetchone()函數(shù)它的返回值是單個的元組, 也就是一行記錄, 如果沒有結果, 那就會返回null
? ? # fetchall()函數(shù)它的返回值是多個元組, 即返回多個行記錄, 如果沒有結果, 返回的是()
? ? # assert result, "數(shù)據(jù)庫無該用戶信息" ? # 添加斷言,判斷數(shù)據(jù)庫有無該用戶信息,沒有就直接斷言錯誤
?
? ? # 登錄賬號操作
? ? name_list = [it[0] for it in result] ? ?# 從數(shù)據(jù)庫查詢的result中遍歷查詢元組中第一個元素name
? ? # 判斷用戶名或密碼不能為空
? ? if not(user_name and user_password):
? ? ? ? tk.messagebox.showwarning(title='警告', message='用戶名或密碼不能為空')
? ? # 判斷用戶名和密碼是否匹配
? ? elif user_name in name_list:
? ? ? ? if user_password == result[name_list.index(user_name)][1]:
? ? ? ? ? ? tk.messagebox.showinfo(title='歡迎您', message=' ? ? ? 登錄成功!\r\n當前登錄賬號為:' + user_name)
? ? ? ? ? ? selection()
? ? ? ? else:
? ? ? ? ? ? tk.messagebox.showerror(title='錯誤', message='密碼輸入錯誤')
? ? # 賬號不在數(shù)據(jù)庫中,則彈出是否注冊的框
? ? else:
? ? ? ? is_signup = tk.messagebox.askyesno(title='提示', message='該賬號不存在,是否現(xiàn)在注冊?')
? ? ? ? if is_signup:
? ? ? ? ? ? user_register()
?
?
# 注冊函數(shù)
def user_register():
? ? # 確認注冊函數(shù)
? ? def register_confirm():
? ? ? ? # 獲取輸入框內(nèi)的內(nèi)容
? ? ? ? name = new_name.get()
? ? ? ? password = new_password.get()
? ? ? ? password_confirm = new_password_confirm.get()
? ? ? ? # 先在本地手動創(chuàng)建一個test_sql數(shù)據(jù)庫,然后連接該數(shù)據(jù)庫
? ? ? ? conn = pymysql.connect(host="localhost", user="root", password="123456", database="test_sql", charset="utf8")
? ? ? ? curs = conn.cursor()
?
? ? ? ? # 注冊賬號操作
? ? ? ? try:
? ? ? ? ? ? # 執(zhí)行SQL語句,創(chuàng)建user數(shù)據(jù)表
? ? ? ? ? ? curs.execute(test_sql)
? ? ? ? ? ? # 向user數(shù)據(jù)表中插入語句
? ? ? ? ? ? insert_sql = "INSERT INTO user(name, password) VALUES ('%s', '%s')" % (name, password)
? ? ? ? ? ? # 讀取user數(shù)據(jù)表中的name和password字段值
? ? ? ? ? ? read_sql = f'''select * from user where name = "{name}" and password = "{password}" '''
? ? ? ? ? ? user_data = curs.execute(read_sql)
? ? ? ? ? ? # 判斷注冊賬號和密碼
? ? ? ? ? ? if not (name and password):
? ? ? ? ? ? ? ? tk.messagebox.showwarning(title='警告', message='注冊賬號或密碼不能為空')
? ? ? ? ? ? elif password != password_confirm:
? ? ? ? ? ? ? ? tk.messagebox.showwarning(title='警告', message='兩次密碼輸入不一致,請重新輸入')
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? if user_data.real:
? ? ? ? ? ? ? ? ? ? tk.messagebox.showwarning(title='警告', message='該注冊賬號已存在')
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? curs.execute(insert_sql)
? ? ? ? ? ? ? ? ? ? tk.messagebox.showinfo(title='恭喜您', message=' ? ? ?注冊成功!\r\n注冊賬號為:' + name)
? ? ? ? ? ? ? ? ? ? print("數(shù)據(jù)插入成功")
? ? ? ? ? ? # 提交到數(shù)據(jù)庫執(zhí)行
? ? ? ? ? ? conn.commit()
? ? ? ? ? ? curs.close()
? ? ? ? except IOError:
? ? ? ? ? ? print("數(shù)據(jù)插入失敗")
? ? ? ? ? ? conn.rollback()
? ? ? ? # 關閉數(shù)據(jù)庫連接
? ? ? ? conn.close()
? ? ? ? window_sign_up.destroy()
?
? ? # 注冊窗口
? ? window_sign_up = tk.Toplevel(window)
? ? window_sign_up.geometry('350x200')
? ? window_sign_up.title('歡迎注冊')
?
? ? # 注冊賬號及標簽、輸入框
? ? new_name = tk.StringVar()
? ? tk.Label(window_sign_up, bg='green', text='注冊賬號:').place(x=50, y=10)
? ? tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
?
? ? # 注冊密碼及標簽、輸入框
? ? new_password = tk.StringVar()
? ? tk.Label(window_sign_up, bg='green', text='密 ? ? ?碼:').place(x=50, y=50)
? ? tk.Entry(window_sign_up, textvariable=new_password, show='*').place(x=150, y=50)
?
? ? # 重復密碼及標簽、輸入框
? ? new_password_confirm = tk.StringVar()
? ? tk.Label(window_sign_up, bg='green', text='確認密碼:').place(x=50, y=90)
? ? tk.Entry(window_sign_up, textvariable=new_password_confirm, show='*').place(x=150, y=90)
?
? ? # 確認注冊按鈕及位置
? ? bt_confirm_sign_up = tk.Button(window_sign_up, bg='green', text='確認注冊', command=register_confirm)
? ? bt_confirm_sign_up.place(x=150, y=130)
?
?
# 選擇題函數(shù)
def selection():
?
? ? def wrong():
? ? ? ? tk.messagebox.showerror(title='錯誤', message='抱歉,您答錯了')
?
? ? def right():
? ? ? ? tk.messagebox.showinfo(title='提示', message='恭喜您,答對了')
?
? ? # 選擇題窗口
? ? window_options = tk.Toplevel(window)
? ? window_options.geometry('350x200')
? ? window_options.title('選擇題')
? ? # 在圖形界面上創(chuàng)建一個標簽label用以顯示并放置
? ? var = tk.StringVar() ?# 定義一個var用來將radiobutton的值和Label的值聯(lián)系在一起.
? ? lab = tk.Label(window_options, bg='red', fg='white', width=50)
? ? lab.pack()
? ? lab.config(text='第1題:兩個銳角均為60度的三角形是什么三角形()' + var.get())
? ? # 創(chuàng)建3個radiobutton選項,其中variable=var, value='A'表示:當鼠標選中其中一個選項,把value的值A放到變量var中,然后賦值給variable
? ? radio1 = tk.Radiobutton(window_options, text='A:銳角三角形', variable=var, value='A', command=wrong)
? ? radio1.pack()
? ? radio2 = tk.Radiobutton(window_options, text='B:鈍角三角形', variable=var, value='B', command=wrong)
? ? radio2.pack()
? ? radio3 = tk.Radiobutton(window_options, text='C:等邊三角形', variable=var, value='C', command=right)
? ? radio3.pack()
? ? radio4 = tk.Radiobutton(window_options, text='D:直角三角形', variable=var, value='D', command=wrong)
? ? radio4.pack()
?
?
# 注冊和登錄按鈕
bt_register = tk.Button(window, bg='yellow', text='注冊', command=user_register)
bt_register.place(x=380, y=300)
bt_login = tk.Button(window, bg='yellow', text='登錄', command=user_login)
bt_login.place(x=440, y=300)
?
# 主循環(huán)
window.mainloop()

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • python 讀取串口數(shù)據(jù)的示例

    python 讀取串口數(shù)據(jù)的示例

    這篇文章主要介紹了python 讀取串口數(shù)據(jù)的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • Python+pyecharts繪制雙動態(tài)曲線教程詳解

    Python+pyecharts繪制雙動態(tài)曲線教程詳解

    pyecharts 是一個用于生成 Echarts 圖表的類庫。Echarts 是百度開源的一個數(shù)據(jù)可視化 JS 庫。用 Echarts 生成的圖可視化效果非常棒。本文將用pyecharts繪制雙動態(tài)曲線,需要的可以參考一下
    2022-06-06
  • python 為什么說eval要慎用

    python 為什么說eval要慎用

    這篇文章主要介紹了python 為什么說eval要慎用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • PYcharm 激活方法(推薦)

    PYcharm 激活方法(推薦)

    這篇文章主要介紹了PYcharm 激活方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Python字符串字母大小寫轉換的各種情況詳析

    Python字符串字母大小寫轉換的各種情況詳析

    在使用python語言開發(fā)中經(jīng)常會碰到,需要大寫轉小寫,小寫轉換大寫,甚至字符串中的單詞首字母大寫,以及字符串手字字母大寫的問題,下面這篇文章主要給大家介紹了關于Python字符串字母大小寫轉換的相關資料,需要的朋友可以參考下
    2022-05-05
  • python2和python3的輸入和輸出區(qū)別介紹

    python2和python3的輸入和輸出區(qū)別介紹

    這篇文章主要介紹了python2和python3的輸入和輸出區(qū)別介紹,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • 對python遍歷文件夾中的所有jpg文件的實例詳解

    對python遍歷文件夾中的所有jpg文件的實例詳解

    今天小編就為大家分享一篇對python遍歷文件夾中的所有jpg文件的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 利用python繪制二三維曲面和矢量流線圖的代碼示例

    利用python繪制二三維曲面和矢量流線圖的代碼示例

    這篇文章主要給大家詳細介紹了如何利用python繪制二三維曲面和矢量流線圖,文中通過代碼示例介紹的非常詳細,對我們學習或工作有一定的幫助,需要的朋友可以參考下
    2023-07-07
  • PyTorch-Forecasting一個新的時間序列預測庫使用詳解

    PyTorch-Forecasting一個新的時間序列預測庫使用詳解

    這篇文章主要為大家介紹了PyTorch-Forecasting一個新的時間序列預測庫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • python opencv實現(xiàn)簡易畫圖板

    python opencv實現(xiàn)簡易畫圖板

    這篇文章主要為大家詳細介紹了python opencv實現(xiàn)簡易畫圖板,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08

最新評論

谢通门县| 慈溪市| 邛崃市| 峡江县| 余江县| 宁安市| 崇文区| 三江| 洛川县| 德江县| 巨野县| 西丰县| 合水县| 东安县| 临邑县| 邢台县| 格尔木市| 新乡市| 遂宁市| 浦东新区| 镇原县| 海伦市| 贵州省| 石城县| 莎车县| 鹤壁市| 丁青县| 永新县| 兴宁市| 裕民县| 湖口县| 沙坪坝区| 邢台市| 杨浦区| 渑池县| 什邡市| 保亭| 九台市| 长汀县| 北海市| 水城县|