一.实验目标
(1) 掌握python图形图像编程
(2) 掌握GUI中的基本组件和布局
(3) 掌握GUI中的事件处理
二.实验内容
import tkinter
import tkinter.ttk
import tkinter.messagebox
win = tkinter.Tk()
win.title('考试系统注册')
win.geometry("440x360")
varName = tkinter.StringVar()
varName.set('')
labelName = tkinter.Label(win,text="学生姓名:",justify=tkinter.LEFT,width=10)
labelName.grid(row=1,column=1)
entryName = tkinter.Entry(win,width=14,textvariable=varName)
entryName.grid(row=1,column=2,pady=5)
labelGrade=tkinter.Label(win,text='省份:',justify=tkinter.RIGHT,width=10)
labelGrade.grid(row=3,column=1)
datas = {'广东省':['A市','B市','C市','D市','E市'],
'辽宁省':['F市','G市','H市','I市','J市'],
'吉林省':['K市','L市','N市','M市','O市']
}
comboPrvince = tkinter.ttk.Combobox(win,width=11,values=tuple(datas.keys()))
comboPrvince.grid(row=3,column=2)
comboCity = tkinter.ttk.Combobox(win,width=11)
comboCity.grid(row=3,column=4)
def comboChange(event):
grade = comboPrvince.get()
if grade:
comboCity["values"] = datas.get(grade)
else :
comboCity.set([])
comboPrvince.bind('<<ComboboxSelected>>',comboChange)
labelClass = tkinter.Label(win,text='地区',justify=tkinter.RIGHT,width=10)
labelClass.grid(row=3,column=3)
labelSex = tkinter.Label(win,text='请选择类别:',justify=tkinter.RIGHT,width=10)
labelSex.grid(row=5,column=1)
stuType = tkinter.IntVar()
stuType.set(1)
radio1 = tkinter.Radiobutton(win,variable=stuType,value=1,text='本科学生')
radio2 = tkinter.Radiobutton(win,variable=stuType,value=0,text='专科学生')
radio1.grid(row=5,column=2,pady=5)
radio2.grid(row=5,column=3)
major = tkinter.IntVar()
major.set(0)
checkmajor = tkinter.Checkbutton(win,text='是否英语专业?',variable=major,onvalue=1,offvalue=0)
checkmajor.grid(row=7,column=1,pady=5)
listboxStudents = tkinter.Listbox(win,width=60)
listboxStudents.grid(row=8,column=1,columnspan=4,padx=5)
def addInformation():
result = '学生姓名:'+entryName.get()
result = result + ';省份:' + comboPrvince.get()
result = result + ';地区:' + comboCity.get()
result = result + ';类别:' + ('本科学生' if stuType.get() else '专科学生')
result = result + ';英语专业:' + ('yes' if major.get() else 'NO')
listboxStudents.insert(0,result)
buttonAdd = tkinter.Button(win,text='增加',width=10,command=addInformation)
buttonAdd.grid(row=7,column=2)
def deleteSelection():
selection = listboxStudents.curselection()
if not selection:
tkinter.messagebox.showinfo(title='删除',width=10,command=deleteSelection)
else :
listboxStudents.delete(selection)
buttonDelete = tkinter.Listbox(win,width=60)
listboxStudents.grid(row=8,column=1,columnspan=4,padx=5)
win.mainloop()
四.测试数据及结果