Python多线程应用

内容:

设计两个界面,分别实现登录和学生信息录入功能。

数据库中创建Uers和student两个表,分别用来存储用户信息及学生信息。

登录界面输入用户名和密码,根据输入内容分别给出“用户名不存在”,“密码错误”等错误提示。正确登录后则进入学生信息录入界面。

学生信息录入界面综合利用文本框,单选钮,复选框,列表框等组件输入学生学号、姓名,并选择所在系、性别等信息,点击“录入”按钮可将该学生信息写入学生表,并在界面的列表框中显示所有学生信息。

模块及方法简介:

Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。

使用Tkinter创建一个GUI程序的步骤:

1、导入 Tkinter 模块

2、创建控件

3、指定这个控件的 master, 即这个控件属于哪一个

4、告诉 GM(geometry manager) 有一个控件产生了。

Tkinter的提供各种控件,如按钮,标签和文本框,一个GUI应用程序中使用。这些控件通常被称为控件或者部件。

此程序用到的组件有:

Button 按钮控件;在程序中显示按钮。

Entry  输入控件;用于显示简单的文本内容

Label   标签控件;可以显示文本和位图

Listbox     列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户

tkMessageBox     用于显示你应用程序的消息框。

思路:

用户登录数据库界面:
创建两个entry,进行用户名和密码的输入。然后使用button,自定义函数检查输入的用户名和密码,先查询数据库users表中的内容,将所有用户名放在列表mz中,将所有密码放到表mm中,判断输入的用户名是否在mz中,如果不在,弹出用户名不存在,否则再判断密码是否在mm中,存在弹出学生信息录入界面,不存在则弹出密码错误。
学生信息录入界面:
使用各种组件按要求建立界面,连接数据库,将输入的信息插入到student表中,然后查询表student,在listbox中显示所有学生信息,要定义一个全局变量x和一个局部变量y,x记录录入信息的条数,y用来表示查询第几条信息,当x等于y时才在listbox插入查询的结果,因为已经显示在listbox中的学生信息就不用再插入了。

代码如下:

#coding=utf-8
import tkinter
import tkinter.messagebox
import tkinter
import tkinter.messagebox
import tkinter.ttk
import sqlite3
x=0
con=sqlite3.connect("users.db")
root = tkinter.Tk()
def Luru():
    root = tkinter.Tk()
    root.title('Selection widgets')
    root['height'] = 400
    root['width'] = 400
    varName = tkinter.StringVar()
    varName.set('')
    varSno=tkinter.StringVar()
    varSno.set('')
    labelName = tkinter.Label(root, text='Name:',justify=tkinter.RIGHT,width=50)
    labelName.place(x=10, y=5, width=50, height=20)
    
    entryName = tkinter.Entry(root, width=120,textvariable=varName)
    entryName.place(x=70, y=5, width=120, height=20)
    
    labelSno = tkinter.Label(root, text='Sno:', justify=tkinter.RIGHT, width=50)
    labelSno.place(x=10, y=40, width=50, height=20)
    
    entrySno = tkinter.Entry(root, width=100,textvariable=varSno)
    entrySno.place(x=70, y=40, width=100, height=20)    
    
    labelDept = tkinter.Label(root, text='Dept:', justify=tkinter.RIGHT, width=50)
    labelDept.place(x=200, y=40, width=40, height=20)
    dept=['cs','ma','en']
    comboDept = tkinter.ttk.Combobox(root, width=40,values=tuple(dept))
    comboDept.place(x=260, y=40, width=40, height=20)
    
    labelSex = tkinter.Label(root, text='Sex:', justify=tkinter.RIGHT, width=50)
    labelSex.place(x=10, y=70, width=50, height=20)
    
    sex = tkinter.IntVar()
    sex.set(1)
    
    radioMan = tkinter.Radiobutton(root,variable=sex,value=1,text='Man')
    radioMan.place(x=70, y=70, width=50, height=20)
    
    radioWoman = tkinter.Radiobutton(root,variable=sex,value=0,text='Woman')
    radioWoman.place(x=130, y=70, width=70, height=20)
    
    def addInformation():
        global x
        x+=1
        y=0
        stu=[]
        stu.append(entrySno.get())
        stu.append(entryName.get())
        if sex.get()==1:
            stu.append("男")
        else:
            stu.append("女")
        stu.append(comboDept.get())
        con.execute("insert into student values(?,?,?,?)",stu)
        result=con.execute("select * from student")
        for i in result:
            y+=1
            if x==y:
                listboxStudents.insert(0,i)
            
    buttonAdd = tkinter.Button(root, text='录入',width=40, command=addInformation)
    buttonAdd.place(x=130, y=100, width=40, height=20)
    
    
    listboxStudents = tkinter.Listbox(root, width=300)
    listboxStudents.place(x=10, y=130, width=300, height=200)
    
    root.mainloop()        

varName = tkinter.StringVar()
varName.set('')
varPwd = tkinter.StringVar()
varPwd.set('')

labelName = tkinter.Label(root, text='User Name:', justify=tkinter.RIGHT, width=80)

labelName.place(x=10, y=5, width=80, height=20)

entryName = tkinter.Entry(root, width=80,textvariable=varName)
entryName.place(x=100, y=5, width=80, height=20)

labelPwd = tkinter.Label(root, text='User Pwd:', justify=tkinter.RIGHT, width=80)
labelPwd.place(x=10, y=30, width=80, height=20)

entryPwd = tkinter.Entry(root, show='*',width=80, textvariable=varPwd)
entryPwd.place(x=100, y=30, width=80, height=20)

def login():
    
    mz=[]
    mm=[]
    name=entryName.get()
    pwd=entryPwd.get()
    result=con.execute("select name,password from users")
    for i in result:
        mz.append(i[0])
        mm.append(i[1])
    if name in mz:
        if pwd in mm:
            Luru()
        else:
            tkinter.messagebox.showinfo(title='Python tkinter',message='密码错误')
    else:
        tkinter.messagebox.showinfo(title='Python tkinter',message='用户名不存在')
buttonOk = tkinter.Button(root, text='Login', command=login)
buttonOk.place(x=30, y=70, width=50, height=20)

def cancel():
    
    varName.set('')
    varPwd.set('')
buttonCancel = tkinter.Button(root, text='Cancel', command=cancel)
buttonCancel.place(x=90, y=70, width=50, height=20)

root.mainloop()



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值