(python)GUI简单的tkinter小应用

书上的小例题

运行结果效果图
这里写图片描述

源码分享

import tkinter
import tkinter.messagebox
import tkinter.ttk
listboxStudents=[]

root=tkinter.Tk()
root.title('Seclection widgets----by Dong Fuguo')
root['height']=400
root['width']=320
lableName=tkinter.Label(root,text='Name:',justify=tkinter.RIGHT,width=50)
lableName.place(x=10,y=5,width=50,height=20)
varName=tkinter.StringVar(value='')
entryName=tkinter.Entry(root,width=120,textvariable=varName)
entryName.place(x=70,y=5,width=50,height=20)
lableGrade=tkinter.Label(root,text='Grade:',justify=tkinter.RIGHT,width=50)
lableGrade.place(x=10,y=40,width=50,height=20)
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=70,y=40,width=50,height=20)
def comboChange(event):
  grade=comboGrade.get()
  if grade:  comboClass["values"]=studentClasses.get(grade)
  else:  comboClass.set([ ])
comboGrade.bind('<<ComboboxSelected>>',comboChange)
lableClass=tkinter.Label(root,text='Class:',justify=tkinter.RIGHT,width=50)
lableClass.place(x=130,y=40,width=50,height=20)
comboClass=tkinter.ttk.Combobox(root,width=50)
comboClass.place(x=190,y=40,width=50,height=20)
lableSex=tkinter.Label(root,text='Sex:',justify=tkinter.RIGHT,width=50)
lableSex.place(x=10,y=70,width=50,height=20)
sex=tkinter.IntVar(value=1)
radioMan=tkinter.Radiobutton(root,variable=sex,value=1,text='Man')
radioMan.place(x=70,y=70,width=70,height=20)
radioWoman=tkinter.Radiobutton(root,variable=sex,value=0,text='Woman')
radioWoman.place(x=130,y=70,width=70,height=20)
monitor=tkinter.IntVar(value=0)
checkMonitor=tkinter.Checkbutton(root,text='Is Monitor?',variable=monitor,onvalue=1,offvalue=0)
checkMonitor.place(x=20,y=100,width=100,height=20)
def addInformation():
    result='Name:'+entryName.get()
    result=result+';Grade:'+comboGrade.get()
    result=result+';Class:'+comboClass.get()
    result=result+';Sex:'+('Man' if sex.get() else 'Woman')
    result=result+';Monitor:'+('yes' if monitor.get() else 'No')
    listboxStudents.insert(0,result)
buttonAdd=tkinter.Button(root,text='Add',width=40,command=addInformation())
buttonAdd.place(x=130,y=100,width=40,height=20)
def deleteSeclection():
    selection=listboxStudents.curselection()
    if not selection:
        tkinter.messagebox.showinfo(title='Infomation',message='No Seclection')
    else:
        listboxStudents.delete(selection)
buttonDelete=tkinter.Button(root,text='DeleteSelection',width=100,command=deleteSeclection)
buttonDelete.place(x=180,y=100,width=100,height=20)
listboxStudents=tkinter.Listbox(root,width=300)
listboxStudents.place(x=10,y=130,width=300,height=200)
root.mainloop()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值