I'm new to Daniweb and with Python and I'm quite bad with it to be honest D:..

Here is my code...

from Tkinter import *

class App(Frame):

    def createWidgets(self):
        self.grid()

        self.lbspace = Label(self, text="")
        self.lbspace.grid(row=0,column=0)

        self.lbfirstName = Label(self, text="First Name:", font=("Calibri", 12))
        self.lbfirstName.grid(row=1,column=0)
        
        self.firstNameVariable = StringVar()
        self.firstName = Entry(self, textvariable=self.firstNameVariable, font=("Calibri", 12))
        self.firstName.grid(row=1,column=1)
        self.firstNameVariable.set("")

        self.lblastName = Label(self, text="Last Name:", font=("Calibri", 12))
        self.lblastName.grid(row=2,column=0)
        
        self.lastNameVariable = StringVar()
        self.lastName = Entry(self, textvariable=self.lastNameVariable, font=("Calibri", 12))
        self.lastName.grid(row=2,column=1)
        self.lastNameVariable.set("")

        self.lbspace = Label(self, text="")
        self.lbspace.grid(row=3,column=0)

        self.lbclassOne = Label(self, text="Class 1:", font=("Calibri", 12))
        self.lbclassOne.grid(row=4,column=0)

        self.classOneVariable = StringVar()
        self.classOne = Entry(self, textvariable=self.classOneVariable, font=("Calibri", 12))
        self.classOne.grid(row=4,column=1)
        self.classOneVariable.set("")

        self.lbclassTwo = Label(self, text="Class 2:", font=("Calibri", 12))
        self.lbclassTwo.grid(row=5,column=0)

        self.classTwoVariable = StringVar()
        self.classTwo = Entry(self, textvariable=self.classTwoVariable, font=("Calibri", 12))
        self.classTwo.grid(row=5,column=1)
        self.classTwoVariable.set("")

        self.lbclassThree = Label(self, text="Class 3:", font=("Calibri", 12))
        self.lbclassThree.grid(row=6,column=0)

        self.classThreeVariable = StringVar()
        self.classThree = Entry(self, textvariable=self.classThreeVariable, font=("Calibri", 12))
        self.classThree.grid(row=6,column=1)
        self.classThreeVariable.set("")

        self.lbclassFour = Label(self, text="Class 4:", font=("Calibri", 12))
        self.lbclassFour.grid(row=7,column=0)

        self.classFourVariable = StringVar()
        self.classFour = Entry(self, textvariable=self.classFourVariable, font=("Calibri", 12))
        self.classFour.grid(row=7,column=1)
        self.classFourVariable.set("")

        self.lbclassFive = Label(self, text="Class 5:", font=("Calibri", 12))
        self.lbclassFive.grid(row=8,column=0)

        self.classFiveVariable = StringVar()
        self.classFive = Entry(self, textvariable=self.classFiveVariable, font=("Calibri", 12))
        self.classFive.grid(row=8,column=1)
        self.classFiveVariable.set("")
        
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.createWidgets()
        frame = Frame()
        menu = Menu(root)
        root.config(menu=menu)
        filemenu = Menu(menu)
        menu.add_cascade(label='File', menu=filemenu)
        filemenu.add_command(label='Open')
        filemenu.add_command(label='Save', command=savefile)
        filemenu.add_command(label='Quit', command=frame.quit)
        infomenu = Menu(menu)
        menu.add_cascade(label='About...', menu=infomenu)
        infomenu.add_command(label='About Student Information Program', command=about)

def savefile(self):
    import time
    text_file=open((self.firstNameVariable.get()) + (self.lastNameVariable.get()) + 'infosave.txt', 'w')
    now = time.localtime(time.time())
    text_file.write("Date is: " +time.asctime(now))
    #("\n") is a new line command for text file
    text_file.write("\n")
    text_file.write(str(self.firstNameVariable.get()))
    text_file.write("\n")
    text_file.write(str(self.lastNameVariable.get()))
    text_file.write("\n")
    text_file.write(str(self.classOneVariable.get()))
    text_file.write("\n")
    text_file.write(str(self.classTwoVariable.get()))
    text_file.write("\n")
    text_file.write(str(self.classThreeVariable.get()))
    text_file.write("\n")
    text_file.write(str(self.classFourVariable.get()))
    text_file.write("\n")
    text_file.write(str(self.classFiveVariable.get()))
    text_file.close()
    

def about():
    print("By ...")
        
        
root = Tk()              
app = App()
app.master.title("Student Information Program")
app.master.minsize(290,250)
root.mainloop()
root.destroy()

Can't get the save to work comes up with um...

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1414, in __call__
return self.func(*args)
TypeError: savefile() takes exactly 1 argument (0 given)

Can anyone help me please? I wouldn't mind any advice on improvements :D.

At first sight, you should indent def savefile():... to make it a method of the App class and use filemenu.add_command(label='Save', command=self.savefile) in the __init__ method.

Oh nice! That worked I think. When I set the default value for the entry boxes it saves. Think I just gotta add a submit button now.

Edit: Forgot to say thanks D:...

Thanks!

Edit 2: Yeah save works fine now thanks once again! Just gotta add a bit more stuff now :O.

Edit 3: Lol another edit... Um is there a way to open a saved text file and have all the saved info like displayed back in the entry boxes or something?

How about

import os
from Tkinter import *
import tkFileDialog

class App(Frame):

    def createWidgets(self):
        self.grid()

        self.lbspace = Label(self, text="")
        self.lbspace.grid(row=0,column=0)

        self.lbfirstName = Label(self, text="First Name:", font=("Calibri", 12))
        self.lbfirstName.grid(row=1,column=0)
        
        self.firstNameVariable = StringVar()
        self.firstName = Entry(self, textvariable=self.firstNameVariable, font=("Calibri", 12))
        self.firstName.grid(row=1,column=1)

        self.lblastName = Label(self, text="Last Name:", font=("Calibri", 12))
        self.lblastName.grid(row=2,column=0)
        
        self.lastNameVariable = StringVar()
        self.lastName = Entry(self, textvariable=self.lastNameVariable, font=("Calibri", 12))
        self.lastName.grid(row=2,column=1)

        self.lbspace = Label(self, text="")
        self.lbspace.grid(row=3,column=0)

        self.lbclassOne = Label(self, text="Class 1:", font=("Calibri", 12))
        self.lbclassOne.grid(row=4,column=0)

        self.classOneVariable = StringVar()
        self.classOne = Entry(self, textvariable=self.classOneVariable, font=("Calibri", 12))
        self.classOne.grid(row=4,column=1)

        self.lbclassTwo = Label(self, text="Class 2:", font=("Calibri", 12))
        self.lbclassTwo.grid(row=5,column=0)

        self.classTwoVariable = StringVar()
        self.classTwo = Entry(self, textvariable=self.classTwoVariable, font=("Calibri", 12))
        self.classTwo.grid(row=5,column=1)

        self.lbclassThree = Label(self, text="Class 3:", font=("Calibri", 12))
        self.lbclassThree.grid(row=6,column=0)

        self.classThreeVariable = StringVar()
        self.classThree = Entry(self, textvariable=self.classThreeVariable, font=("Calibri", 12))
        self.classThree.grid(row=6,column=1)

        self.lbclassFour = Label(self, text="Class 4:", font=("Calibri", 12))
        self.lbclassFour.grid(row=7,column=0)

        self.classFourVariable = StringVar()
        self.classFour = Entry(self, textvariable=self.classFourVariable, font=("Calibri", 12))
        self.classFour.grid(row=7,column=1)

        self.lbclassFive = Label(self, text="Class 5:", font=("Calibri", 12))
        self.lbclassFive.grid(row=8,column=0)

        self.classFiveVariable = StringVar()
        self.classFive = Entry(self, textvariable=self.classFiveVariable, font=("Calibri", 12))
        self.classFive.grid(row=8,column=1)

        self.varlist=[self.firstNameVariable,
                 self.lastNameVariable,
                 self.classOneVariable,
                 self.classTwoVariable,
                 self.classThreeVariable,
                 self.classFourVariable,
                 self.classFiveVariable]
        
        for var in self.varlist: var.set('')

        
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.createWidgets()
        frame = Frame()
        menu = Menu(root)
        root.config(menu=menu)
        filemenu = Menu(menu)
        menu.add_cascade(label='File', menu=filemenu)
        filemenu.add_command(label='Open', command=self.openfile)
        filemenu.add_command(label='Save', command=self.savefile)
        filemenu.add_command(label='Quit', command=root.destroy)
        infomenu = Menu(menu)
        menu.add_cascade(label='About...', menu=infomenu)
        infomenu.add_command(label='About Student Information Program', command=about)

    def savefile(self):
        import time
        text_file=open((self.firstNameVariable.get()) + (self.lastNameVariable.get()) + 'infosave.txt', 'w')
        now = time.localtime(time.time())
        text_file.write("Date is: " +time.asctime(now)+'\n') ## newline was missing
        text_file.write('\n'.join([x.get() for x in self.varlist]))
        text_file.close()
        for var in self.varlist: var.set('') # clear for next student and sign of saving        

    def openfile(self):
        filename = tkFileDialog.askopenfilename()
        
        if os.path.isfile(filename):
            f = list(open(filename,'r'))
            f.pop(0) ## drop date from front
            for var in self.varlist:
                var.set(f.pop(0)) ## from front=0
        else: print 'No file chosen'
        

def about():
    print("By ...")
        
        
root = Tk()              
app = App()
app.master.title("Student Information Program")
app.master.minsize(290,250)
root.mainloop()

Wow awesome! Tried it myself but it didn't work out. Going to study this code now ;D.

Wow awesome! Tried it myself but it didn't work out. Going to study this code now ;D.

I modified little your program so that I could understand the grid style as I have used only pack until now.

import os
from Tkinter import *
import tkFileDialog

class App(Frame):

    def createWidgets(self):
        self.grid()
        font=("Calibri", 12)
        self.varlist=[]
 
        # label, place,inputplace
        self.info=(
            ("",(0,0),None),
            ("First Name:",(1,0),(1,1)),
            ("Last Name:", (2,0),(2,1)),
            ("",(3,0),None),
            ("Class 1:",(4,0),(4,1)),
            ("Class 2:",(5,0),(5,1)),
            ("Class 3:",(6,0),(6,1)),
            ("Class 4:",(7,0),(7,1)),
            ("Class 5:",(8,0),(8,1))
            )
        
        for text,lpos,ipos in self.info:
            print text,lpos,ipos
            if text:
                var=StringVar()
                self.varlist.append(var)
                var.set('')
                
            Label(self,
                  text=text,
                  font=font).grid(row=lpos[0],
                                  column=lpos[1])
            if ipos:
                Entry(self,
                      textvariable = var,
                      font = font).grid( row=ipos[0],
                                         column=ipos[1])
        
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.createWidgets()
        frame = Frame()
        menu = Menu(root)
        root.config(menu=menu)
        filemenu = Menu(menu)
        menu.add_cascade(label='File', menu=filemenu)
        filemenu.add_command(label='Open', command=self.openfile)
        filemenu.add_command(label='Save', command=self.savefile)
        filemenu.add_command(label='Quit', command=root.destroy)
        infomenu = Menu(menu)
        menu.add_cascade(label='About...', menu=infomenu)
        infomenu.add_command(label='About Student Information Program', command=about)

    def savefile(self):
        import time
        text_file=open((self.varlist[0].get()) + (self.varlist[1].get()) + '_inf.txt', 'w')
        now = time.localtime(time.time())
        text_file.write("Date is: " +time.asctime(now)+'\n') ## newline was missing
        text_file.write('\n'.join([x.get() for x in self.varlist]))
        text_file.close()
        for var in self.varlist: var.set('') # clear for next student and sign of saving        

    def openfile(self):
        filename = tkFileDialog.askopenfilename()
        
        if os.path.isfile(filename):
            f = list(open(filename,'r'))
            f.pop(0) ## drop date from front
            for var in self.varlist:
                if f: var.set(f.pop(0)) ## from front=0
        else: print 'No file chosen'
        

def about():
    print("By ...")
        
        
root = Tk()              
app = App()
app.master.title("Student Information Program")
app.master.minsize(290,250)
root.mainloop()
commented: Nice! +0

Can you mark solved or tell what is still unclear/post results?

Oh man your modifications really neat and sorry about not marking thread as solve =S. Haven't been playing around with python lately.

One question I've tried playing around with it and making more entry boxes and making them save integers but it didn't work out...

EDIT: I'm not too sure how line 25-40 works in your modification? Could you please explain:S?

Did you run it and look output from terminal window? It has one unnecessary print statement on line 26 to show you how the iteration goes through the widgets or more properly explanation label and entry box , position of label, position of entry box.

Your file saving of one record per file with individual naming is bit strange though. Usually is used one row in file of database per record.

Ah I sort of understand it now, thanks ;D. I tried to add more entry boxes and make them a difference size then the rest but using the width= under the if ipos: changes them all -__-.

I tried to add more entry boxes and make them a difference size then the rest but using the width= under the if ipos: changes them all -__-.

Just use a separate block of code to add the additional entry boxes that are a different size, or add a width parameter to the info list.

Ah I got it now. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.