【Python基礎編程】tkinter+pymysql 實現數據庫查詢與插入(簡單的登錄與注冊功能)


前言

IDLE中並沒有提供依靠拖動空間搭建窗體應用的環境,需要我們自己去了解一些調用系統接口的模塊,從而去編寫程序。

正文

1.tkinter

tkinter提供了眾多控件

使用的一般規則為:

step1:引入模塊

1 from tkinter import *

step2:實例化一個窗體,實例化控件,擺放控件

 1 #窗體繪制
 2 LoginFrom=Tk()
 3 #實例化控件
 4 lbl1=Label(LoginFrom,text="用戶名")
 5 lbl2=Label(LoginFrom,text="密碼")
 6 txt1=Entry(LoginFrom)
 7 txt2=Entry(LoginFrom)
 8 bttn1=Button(LoginFrom,text="登錄",command=self.log)
 9 bttn2=Button(LoginFrom,text="注冊",command=self.regs)
10 #擺放控件,這里的擺放函數有多種如.grid(),.place()有自己的調整參數,這里簡單擺放即可
11 lbl1.pack()
12 self.txt1.pack()
13 lbl2.pack()
14 self.txt2.pack()
15 bttn1.pack()
16 bttn2.pack()
17 #也可以這樣直接擺放
18 Button(LoginFrom,text="查詢",command=self.search).pack()

像label,Entry等簡單控件實例化格式為:    控件名=控件類型(放在哪個窗體,顯示內容="文字")

像button等控件需要在以上基礎上指定一個函數事件command=函數名

這樣就能運行出一個不那么美觀的窗體

 

 

 

tkniter提供的控件

 

 

 

 

2.pymysql

 pymysql是用於操作mysql數據庫的模塊,像.NET語言中的sqlclient

在使用之前要引入

import pymysql

剩下的事情由connect鏈接對象與cursor游標兌現完成

pymysql鏈接mysql數據庫的步驟有

#指定鏈接參數新建connect對象
conn=pymysql.connect(host="localhost",\
      user="root",password="root",database="newdb",charset="utf8")
#新建cursor sql語句有關的對象
cursor = conn.cursor()
#執行sql語句
sql="select * from admin"
#返回執行狀態
cursor.execute(sql)
#獲取數據
cursor.fetchall()

總文

 

 源代碼

from tkinter import *
from tkinter import messagebox
import pymysql
class Frm:
      conn=pymysql.connect(host="localhost",\
      user="root",password="root",database="newdb",charset="utf8")
      def __init__(self):
            
            LoginFrom=Tk()#窗體繪制
            lbl1=Label(LoginFrom,text="用戶名")
            lbl2=Label(LoginFrom,text="密碼")
            self.txt1=Entry(LoginFrom)
            self.txt2=Entry(LoginFrom)
            bttn1=Button(LoginFrom,text="登錄",command=self.log)
            bttn2=Button(LoginFrom,text="注冊",command=self.regs)
            lbl1.pack()
            self.txt1.pack()
            lbl2.pack()
            self.txt2.pack()
            bttn1.pack()
            bttn2.pack()
            Button(LoginFrom,text="查詢",command=self.search).pack()
            
            LoginFrom.mainloop()
      def search(self):
            cursor = self.conn.cursor()
            print(cursor)
            sql="select * from admin"
            cursor.execute(sql)
            print(cursor.fetchall())
            cursor.close()
      def log(self):
            cursor = self.conn.cursor()
            print(cursor)
            sql="select * from admin where admin_name='"+self.txt1.get()+\
            "'and admin_psw='"+self.txt2.get()+"'"
            if(cursor.execute(sql)):
                  messagebox.showinfo("系統提示","登陸成功")
                  
            else :
                  messagebox.showerror("系統提示","賬號或者密碼錯誤")
            cursor.close()
            
      def regs(self):
            RegsFrom=Tk()
            
            lbl1=Label(RegsFrom,text="用戶名")
            lbl2=Label(RegsFrom,text="密碼")
            self.txt3=Entry(RegsFrom)
            self.txt4=Entry(RegsFrom)
            lbl1.pack()
            self.txt3.pack()
            lbl2.pack()
            self.txt4.pack()
            bttn3=Button(RegsFrom,text="點擊注冊",command=self.regsclick)
            bttn3.pack()
            
      def regsclick(self):
            cursor = self.conn.cursor()
            print(RegsFrom)
            print(cursor)
            sqlin="insert into admin(admin_name,admin_psw) VALUES(%s,%s)"
            
            data = [
                (self.txt3.get(), self.txt4.get())]
            if(cursor.executemany(sqlin,data)):
                  messagebox.showinfo("系統提示","注冊成功")
                  print(self.txt3.get())
                  #這一步是插入語句中至關重要的的一部分,查詢語句可以沒有但是插入更新刪除等語句要有
                  self.conn.commit()

            cursor.close()
      def quit(frm):
            frm.destroy()
if __name__=='__main__':
      
     a=Frm()

                                

運行結果展示

 

 按登錄,由於沒有寫登錄后的窗口只會提示這個

 

 按注冊

彈出新的窗體

 

 注冊功能

 

 

數據庫中查看

 

 查詢--打印在控制台上

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM