import tkinter
import tkinter.messagebox
import tkinter.ttk
classes={'1':['z1','z2','z3','z4'],
'2':['l1','l2','l3'],
'3':['m5','m6','m7','m8']}
root=tkinter.Tk()
root.title("GUI TWO")
root['height']=300
root['width']=270
def changeClass(event):
grade=comboGrade.get()
if grade:
comboClass['values']=classes.get(grade)
else:
comboClass['values']=[]
def add():
name=entryName.get()
grade=comboGrade.get()
cla=comboClass.get()
if not (name.strip() and grade and cla):
tkinter.messagebox.showerror('Add denied','information is not completed!')
mess=name+";"+grade+";"+cla+";"+('Man' if sex.get() else 'Woman')+";"+('Y' if varLeader.get() else 'N')
listboxData.insert(0,mess)
print(mess.split(";"))
def deleteItem():
currentItem=listboxData.curselection()
if currentItem:
listboxData.delete(currentItem)
else:
tkinter.messagebox.showinfo(title="Delete denied", message="selected item is null!")
#label:name
labelName=tkinter.Label(root,
text='Name:',
justify=tkinter.RIGHT,
width=50)
labelName.place(x=10,y=5,width=50,height=20)
#input:name
varName=tkinter.StringVar(root,value='')
entryName=tkinter.Entry(root,
width=80,
textvariable=varName)
entryName.place(x=70,y=5,width=150,height=20)
#label:grade
labelGrade=tkinter.Label(root,
text='Grade:',
justify=tkinter.RIGHT,
width=50)
labelGrade.place(x=10,y=40,width=50,height=20)
#combobox:grade
comboGrade=tkinter.ttk.Combobox(root,
width=50,
values=tuple(classes.keys()))
comboGrade.place(x=70,y=40,width=50,height=20)
#label:class
labelClass=tkinter.Label(root,
text='Class:',
justify=tkinter.RIGHT,
width=50)
labelClass.place(x=150,y=40,width=50,height=20)
#combobox:class
comboClass=tkinter.ttk.Combobox(root,width=50)
comboClass.place(x=200,y=40,width=50,height=20)
#label:sex
labelSex=tkinter.Label(root,
text='Sex:',
justify=tkinter.RIGHT,
width=50)
labelSex.place(x=5,y=70,width=50,height=20)
#default sex
sex=tkinter.IntVar(root,value=1)
#radioButton:man
radioMan=tkinter.Radiobutton(root,
text='Man',
value=1,
variable=sex)
radioMan.place(x=70,y=70,width=50,height=20)
#radioButton:woman
radioWoman=tkinter.Radiobutton(root,
text='Woman',
value=0,
variable=sex)
radioWoman.place(x=140,y=70,width=90,height=20)
#check:leader
varLeader=tkinter.IntVar(root,value=0)
checkLeader=tkinter.Checkbutton(root,
text='is leader?',
variable=varLeader,
onvalue=1,
offvalue=0)
checkLeader.place(x=10,y=100,width=90,height=20)
#button:add
buttonAdd=tkinter.Button(root,
text='Add',
width=40,
command=add)
buttonAdd.place(x=150,y=100,width=40,height=20)
#button:delete
buttonDelete=tkinter.Button(root,
text='Delete',
width=80,
command=deleteItem)
buttonDelete.place(x=200,y=100,width=50,height=20)
#listbox
listboxData=tkinter.Listbox(root,width=300)
listboxData.place(x=15,y=130,width=235,height=150)
comboGrade.bind('<<ComboboxSelected>>',changeClass)
root.mainloop()
python的簡單GUI(數據綁定與listbox)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.