您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在python中利用tkinter實現一個學生管理系統,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
Python是一種編程語言,內置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強大,在許多領域中都有廣泛的應用,例如最熱門的大數據分析,人工智能,Web開發等。
具體內容如下
from tkinter import * from tkinter.messagebox import * import sqlite3 from tkinter import ttk dbstr = "H:\mydb.db" root = Tk() root.geometry('700x1000') root.title('學生管理系統') Label(root, text="學號:").place(relx=0, rely=0.05, relwidth=0.1) Label(root, text="姓名:").place(relx=0.5, rely=0.05, relwidth=0.1) Label(root, text="電話:").place(relx=0, rely=0.1, relwidth=0.1) Label(root, text="地址:").place(relx=0.5, rely=0.1, relwidth=0.1) sid = StringVar() name = StringVar() phone = StringVar() address = StringVar() Entry(root, textvariable=sid).place(relx=0.1, rely=0.05, relwidth=0.37, height=25) Entry(root, textvariable=name).place(relx=0.6, rely=0.05, relwidth=0.37, height=25) Entry(root, textvariable=phone).place(relx=0.1, rely=0.1, relwidth=0.37, height=25) Entry(root, textvariable=address).place(relx=0.6, rely=0.1, relwidth=0.37, height=25) Label(root, text='學生信息管理', bg='white', fg='red', font=('宋體', 15)).pack(side=TOP, fill='x') def showAllInfo(): x = dataTreeview.get_children() for item in x: dataTreeview.delete(item) con = sqlite3.connect(dbstr) cur = con.cursor() cur.execute("select * from student") lst = cur.fetchall() for item in lst: dataTreeview.insert("", 1, text="line1", values=item) cur.close() con.close() def appendInfo(): if sid.get() == "": showerror(title='提示', message='輸入不能為空') elif name.get() == "": showerror(title='提示', message='輸入不能為空') elif phone.get() == "": showerror(title='提示', message='輸入不能為空') elif address.get() == "": showerror(title='提示', message='輸入不能為空') else: x = dataTreeview.get_children() for item in x: dataTreeview.delete(item) list1 = [] list1.append(sid.get()) list1.append(name.get()) list1.append(phone.get()) list1.append(address.get()) con = sqlite3.connect(dbstr) cur = con.cursor() cur.execute("insert into student values(?,?,?,?)", tuple(list1)) con.commit() cur.execute("select * from student") lst = cur.fetchall() for item in lst: dataTreeview.insert("", 1, text="line1", values=item) cur.close() con.close() def deleteInfo(): con = sqlite3.connect(dbstr) cur = con.cursor() cur.execute("select * from student") studentList = cur.fetchall() cur.close() con.close() print(studentList) num = sid.get() flag = 0 if num.isnumeric() == False: showerror(title='提示', message='刪除失敗') for i in range(len(studentList)): for item in studentList[i]: if int(num) == item: flag = 1 con = sqlite3.connect(dbstr) cur = con.cursor() cur.execute("delete from student where id = ?", (int(num),)) con.commit() cur.close() con.close() break if flag == 1: showinfo(title='提示', message='刪除成功!') else: showerror(title='提示', message='刪除失敗') x = dataTreeview.get_children() for item in x: dataTreeview.delete(item) con = sqlite3.connect(dbstr) cur = con.cursor() cur.execute("select * from student") lst = cur.fetchall() for item in lst: dataTreeview.insert("", 1, text="line1", values=item) cur.close() con.close() Button(root, text="顯示所有信息", command=showAllInfo).place(relx=0.2, rely=0.2, width=100) Button(root, text="追加信息", command=appendInfo).place(relx=0.4, rely=0.2, width=100) Button(root, text="刪除信息", command=deleteInfo).place(relx=0.6, rely=0.2, width=100) dataTreeview = ttk.Treeview(root, show='headings', column=('sid', 'name', 'phone', 'address')) dataTreeview.column('sid', width=150, anchor="center") dataTreeview.column('name', width=150, anchor="center") dataTreeview.column('phone', width=150, anchor="center") dataTreeview.column('address', width=150, anchor="center") dataTreeview.heading('sid', text='學號') dataTreeview.heading('name', text='名字') dataTreeview.heading('phone', text='電話') dataTreeview.heading('address', text='地址') dataTreeview.place(rely=0.3, relwidth=0.97)
關于怎么在python中利用tkinter實現一個學生管理系統就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。