(作者一大学生,学习很紧,熬夜到一点拿出时间写这个很不容易,学了一段时间python略有所得,这道典例是我修改了一些董付国老师参考书《Python编程基础》中的一道例题,作为新手入门GUI教程(我也才学python不太长)。相对书上的例题,此代码将其写入文件,保证了其运行之后可以保存,并且每次打开之后都是以前写过的内容,相较于那个习题解决了一些一般性编程的一些问题:)
也是最近才上GUI AWA
相关的教程在注释里非常详细
包括创建按钮,选择框,定义单重选择框,并对选框内容进行动态添加,删除和提取内容,包括操作文件,写入,添加,删除指定的行,清空选框和文件,设置弹窗消息,等等,为了这个我也参考了许多CSDN上其他文章的内容
因为学习紧张就不在上面讲太多了
首先正常运行这个需要在d盘保存一个空的Student_Info.txt文件
然后直接上代码(注释都有了,不说了)
import tkinter
import tkinter.ttk
import tkinter.messagebox
import os
root=tkinter.Tk()
root.title("The Application of Section widgets")
root['height']=500
root['width']=800
Info=tkinter.Label(root,text='Input your information:',font=20)
Info.place(x=150,y=60,width=200,height=30)
labelName=tkinter.Label(root,text='Name:',justify=tkinter.RIGHT,width=50)
labelName.place(x=100,y=100,width=50,height=30)
varName=tkinter.StringVar(value='')
entryName=tkinter.Entry(root,width=150)
entryName.place(x=150,y=100,width=150,height=30)
labelGrade=tkinter.Label(root,text='Grade:',justify=tkinter.RIGHT,width=50)
labelGrade.place(x=100,y=150,width=50,height=30)
#===============制作“连锁型”选框====================
studentClasses={'1':['1','2','3','4','培优班'],
'2':['1','2','英才班'], #字典键为年级
'3':['1','2','3','火箭班']} #字典值为班级
#利用字典的键和值来实现多选框的“动态改变第二个框的分类选项”
comboGrade=tkinter.ttk.Combobox(root,values=tuple(studentClasses.keys()),width=50)
comboGrade.place(x=150,y=150,width=50,height=30)
#----------------------------------------------------------
def comboChange(event): #定义事件处理函数
grade=comboGrade.get() #从该选框中得到年级的值
if grade:
comboClass["values"]=tuple(studentClasses[str(grade)])
#注意!values值必须是tuple,且字典取其键时直接用studentClasses[i]
else:
comboClass["values"].set([])
comboGrade.bind('<<ComboboxSelected>>',comboChange)
#使用bind绑定事件处理函数,下拉列表的虚拟事件是<<ComboboxSelected>>
#*****----不绑定会出错-------******#
#----------------------------------------------------------------------------
comboClass=tkinter.ttk.Combobox(root,width=100)
#班级选框,其中的值由上方的函数所决定
comboClass.place(x=150,y=200,width=100,height=30)
#=================
labelClass=tkinter.ttk.Combobox()
labelClass=tkinter.Label(text='Class:',justify=tkinter.RIGHT,width=50)
labelClass.place(x=100,y=200,width=50,height=30)
#年级和班级的复选形式
#------------------------------------------------------
#==========设置对勾选框组=========
labelSex=tkinter.Label(text='sex:',width=50)
labelSex.place(x=100,y=250,width=50,height=30)
sex=tkinter.IntVar(root,value=0) #用intvar来选择,0表示男,1表示女
radioMan=tkinter.Radiobutton(root,value=0,variable=sex,text='man')
radioMan.place(x=150,y=250,width=50,height=30)
radioWom=tkinter.Radiobutton(root,value=1,variable=sex,text='woman')
radioWom.place(x=250,y=250,width=100,height=30)
'''*****注意使用variable=sex来取得选择的值,Radiobutton是按钮组
通过Radiobutton来设置相应的参数对应的值******'''
#====================================
monitor=tkinter.IntVar(root,value=0)
checkMonitor=tkinter.Checkbutton(text='monitor',variable=monitor,onvalue=1,offvalue=0)
checkMonitor.place(x=120,y=300,width=100,height=30)
#-----将"添加人员"作为按钮事件并处理-----------
def addInfomation():
if entryName.get()!='' and comboGrade.get()!=None and\
comboClass.get()!=None:
result="Name:"+entryName.get()
result=result+';Grade:'+comboGrade.get()
result=result+';Class:'+comboClass.get()
result=result+';Sex:'+('Woman' if sex.get() else 'Man')
result=result+';Monitor:'+('Yes'if monitor.get() else 'No')+'\n'
#使用insert动态改变复选框
with open(r'd:\Student_Info.txt','r+') as fp:
s=0
for line in fp.readlines():
if result==line:
tkinter.messagebox.showinfo(title='输入信息重复',message='您已经输入过您的信息,请勿重复输入!')
s+=1
break
if s==0:
Student.insert(0,result)
fp.write(result)
else:
tkinter.messagebox.showinfo(title='输入错误',message='请输入完整的个人信息!')
#这里写入文件进行处理
buttonAdd=tkinter.Button(text='add student infomation',width=200,command=addInfomation)
buttonAdd.place(x=100,y=350,width=200,height=30)
SI=tkinter.Label(text='Student Infomation In total:',width=250)
SI.place(x=450,y=50,width=250,height=30)
#定义复选框.......
Student=tkinter.Listbox(root,selectmode=tkinter.BROWSE)
'''selectmode中,BROWSE是一次选一行(可拖动)(为了配合后面的delete)
另外,MULTIPLE是多项选择,SINGLE是不可拖动单项选择,
EXTEND是可选中多行且可拖动'''
with open(r'd:\Student_Info.txt','r') as fp:
for line in fp.readlines():
Student.insert(tkinter.END,line)
Student.place(x=400,y=80,width=350,height=300)
#将该文件的每行作为列表中的一行,通过选中进行删除
#----------------定义删除所有列表的选项按钮----------
def deleteAllSelection():
Student.delete(0,tkinter.END)
with open(r'd:\Student_Info.txt','w') as fp:
fp.write('')
#如此删除所有列表中的内容并清空文件
RemoveButton=tkinter.Button(text='Delete All Student_Info',width=200,command=deleteAllSelection)
#对于按钮,利用command执行按钮中的内容
RemoveButton.place(x=475,y=430,width=200,height=30)
#---------------------------------------------------
def deleteSelected():
selection=Student.curselection()
string=Student.get(Student.curselection())
#注意:--这里利用Student.get实现获取选中框中的文本-------
Student.delete(selection)
#删除复选框的选项
#======开始处理对应文件-------必须保证_new创建时为空文件夹=============
try:
os.remove(r'd:\Student_Info_new.txt')
except:
pass
#修改文件中的内容
#利用重写文件的方式删去文件中的某一行
with open(r'd:\Student_Info.txt','r+') as fp: #r+是读写模式
with open(r'd:\Student_Info_new.txt','a+') as fn:
for line in fp.readlines():
if line==string:
pass
else:
fn.write(line)
os.remove(r'd:\Student_Info.txt')
with open(r'd:\Student_Info.txt','a+') as fp:
with open(r'd:\Student_Info_new.txt','r') as fn:
for line in fn.readlines():
fp.write(line)
#将原来的内容写回该文件
#(一开始我试过在'w'下fp.write(fn.read())但总是出错
deleteButton=tkinter.Button(text='Delete Selected Student_Info',width=200,command=deleteSelected)
deleteButton.place(x=475,y=390,width=200,height=30)
root.mainloop()
运行截图
如果讲的确实还是有难点,可以在评论区里@我
确实不能再熬了
zzz