Python 2.7 Tutorial Pt 16

Python 2.7 TutorialHere I continue to teach you how to use the Tkinter GUI module built into Python. I build an application that allows you to input files, edit them and then save them. I specifically show you how to make the following GUI widgets with Tkinter:

  • Menu Bars
  • Multi-Line Editable Text Boxes
  • Drop Down Menus

If you haven’t seen the previous Tkinter tutorial you should definitely check it out below first. Otherwise this won’t make any sense.

Leave any questions or comments below and here are all of my previous Python Video Tutorial’s:


[adsense]

Important Note

I was going to originally cover Django in this tutorial, but because many of the major hosting companies don’t allow it to be easily installed, I’m going to pass. I’m also going to finish my Python tutorial very soon. The next tutorial will be on one or another of the following subjects:

  • PHP Security, MySQL, and WordPress Themes & Plugins
  • iPhone Development

Vote for your favorite and which ever subject gets the most votes wins!

Here is All the Code from the Python Video Tutorial

from Tkinter import *
import tkinter.messagebox

def openGrades():
f= open(“studentgrades.txt”)
aboutStud.delete(1.0,END)

studGradeString = “”

for i in f:
studGradeString += i

aboutStud.insert(END, studGradeString)
f.close()
return

def openRank():
f= open(“studentrank.txt”)
aboutStud.delete(1.0,END)

studGradeString = “”

for i in f:
studGradeString += i

aboutStud.insert(END, studGradeString)
f.close()
return

def openFiles(selection):
if selection == “Student Grades”:
openGrades()
else: openRank()
return

def saveGrades():
gradeUpdate = aboutStud.get(1.0,END)
outToFile = open(‘studentgrades.txt’,’w’)
outToFile.write(gradeUpdate)
outToFile.close()
return

def aboutMe():
tkinter.messagebox.showinfo(“Hello”)
return

app = Tk()
app.title(“Gui Example”)
app.geometry(“560×460+200+200″)

menubar = Menu(app)
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(label=”Open Grades”,command=openGrades)
filemenu.add_command(label=”Open Rank”, command=openRank)

filemenu.add_separator()

filemenu.add_command(label=”Quit”, command=app.quit)
menubar.add_cascade(label=”File”,menu=filemenu)

helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_cascade(label=”About Me”, command=aboutMe)
menubar.add_cascade(label=”Help”,menu=helpmenu)

app.config(menu=menubar)

aboutStud = Text(app)
aboutStud.insert(END, “Select student information above”)
aboutStud.pack()

studFiles = StringVar()
studFiles.set(None)
files = [‘Student Grades’, “Student Rank”]
studDropDown = OptionMenu(app, studFiles, *files, command=openFiles).pack()

button1 = Button(app, text=”Save Grades”, width = 20, command=saveGrades)
button1.pack(side=’bottom’,padx=15,pady=15)

app.mainloop()

10 Responses to “Python 2.7 Tutorial Pt 16”

  1. mak says:

    Hi,

    your training awesome, but small request here, it would be great if show the format of txt [studentgrades and rank] files. And where to locate those files. Bcoz am trying run this on linux, but it always throws error for location path. Its too helpful for me if you can help on this.

    Thanks in Advance,
    Mak

  2. Chris says:

    this is a great example. Do you have one where a user can get any file from any directory?

  3. Avi Drucker says:

    Hi Derek, would you please make the student grades and rank text files (or their contents) available? Thank you very much!

Leave a Reply

Your email address will not be published.

Google+