Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. from Tkinter import Tk,Button,StringVar,Entry,Label
  2. import random, tkFileDialog, tkMessageBox
  3. root= Tk()
  4. root.wm_title("Random Student Selector")
  5. current=StringVar()
  6. kids=[]
  7. addKidName=Entry(root)
  8. def addkid():
  9. kids.append(addKidName.get())
  10. addKid=Button(root, text="Add a student", command=addkid)
  11. def choosekid():
  12. try:
  13. chosen=random.choice(kids)
  14. except IndexError:
  15. tkMessageBox.showinfo("Empty class", "You need to add a student")
  16. current.set(chosen)
  17. chooseKid=Button(root,text="Pick random student", command=choosekid)
  18. chosenKid=Label(root,textvariable=current)
  19. def loadfile():
  20. global kids
  21. loadedfile=tkFileDialog.askopenfile(mode='r', filetypes=[("Text files","*.ss")])
  22. try:
  23. kids=loadedfile.read().split(",")
  24. except AttributeError:
  25. tkMessageBox.showinfo("No file selected", "You need to select a file")
  26. loadFile=Button(root,text="Load a class",command=loadfile)
  27. def savetofile():
  28. savefile=tkFileDialog.asksaveasfile()
  29. stringToSave=""
  30. for i in kids:
  31. stringToSave=stringToSave+i+","
  32. stringToSave=stringToSave[:-1]
  33. savefile.write(stringToSave)
  34. saveToFile=Button(root,text="Save a file",command=savetofile)
  35. addKid.grid(row=0,column=1)
  36. addKidName.grid(row=0,column=0)
  37. chooseKid.grid(row=1,column=1)
  38. chosenKid.grid(row=1,column=0)
  39. loadFile.grid(row=2,column=0)
  40. saveToFile.grid(row=2,column=1)
  41. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement