This selects a random student from a class list, stored in a .ss file:
from Tkinter import Tk,Button,StringVar,Entry,Label
import random, tkFileDialog, tkMessageBox
root= Tk()
root.wm_title("Random Student Selector")
current=StringVar()
kids=[]
addKidName=Entry(root)
def addkid():
kids.append(addKidName.get())
addKid=Button(root, text="Add a student", command=addkid)
def choosekid():
try:
chosen=random.choice(kids)
except IndexError:
tkMessageBox.showinfo("Empty class", "You need to add a student")
current.set(chosen)
chooseKid=Button(root,text="Pick random student", command=choosekid)
chosenKid=Label(root,textvariable=current)
def loadfile():
global kids
loadedfile=tkFileDialog.askopenfile(mode='r', filetypes=[("Text files","*.ss")])
try:
kids=loadedfile.read().split(",")
except AttributeError:
tkMessageBox.showinfo("No file selected", "You need to select a file")
loadFile=Button(root,text="Load a class",command=loadfile)
def savetofile():
savefile=tkFileDialog.asksaveasfile()
stringToSave=""
for i in kids:
stringToSave=stringToSave+i+","
stringToSave=stringToSave[:-1]
savefile.write(stringToSave)
saveToFile=Button(root,text="Save a file",command=savetofile)
addKid.grid(row=0,column=1)
addKidName.grid(row=0,column=0)
chooseKid.grid(row=1,column=1)
chosenKid.grid(row=1,column=0)
loadFile.grid(row=2,column=0)
saveToFile.grid(row=2,column=1)
root.mainloop()
1 Answer 1
First off, things I like about this code:
- you don't use
StringVars
for yourEntry
widgets. Lots of tutorials seem to use them, but they are largely unnecessary - I like that you grouped all of your
grid
commands together.
Now, the things I think need improving:
from Tkinter import Tk,Button,StringVar,Entry,Label
Best practices suggest you import the whole library with a shorthand name such as tk
. You then can reference each of the widgets by the short name to make it absolutely clear where the code is coming from. Otherwise, if you were to use both Tkinter and ttk widgets at some point, it becomes unclear if you're referencing a Tkinter widget or a ttk widget since they both export similar class names.
I suggest changing your import to this:
import Tkinter as tk
Then, change all references to Button
to be tk.Button
, etc. This extra little bit of typing will make your code much easier to read and maintain over time.
It's great that you grouped all of your grid
statements together. However, the rest of your code is oddly interleaved. You have functions intermixed with code.
Instead of doing this:
addKidName=Entry(...)
def addkid(): ...
addKid=Button(...)
def choosekid(): ...
group all of your functions together, and all your widgets together.
def addkid(): ...
def choosekid(): ...
def loadfile(): ...
def savetofile(): ...
...
addKidName=Entry(...)
addKid=Button(...)
...
If you use grid
you should always do three things:
1) explicitly declare a sticky
attribute for each widget
2) always use rowconfigure
to give at least one row a positive, non-zero weight (even if it's an invisible row below all other rows)
3) always use columnconfigure
to give at least one column a positive, non-zero weight (even if it's an invisible column to the right of all other columns)
def
to make it more readble. \$\endgroup\$root = Tk()
, etc.). \$\endgroup\$