0

This is the code that I have so far. Towards the end I'm having troubles figuring out how to make a popup window saying "Grade is [A]", once the student ID is entered. I also don't know how to create a popup window saying "Student ID not found" if an incorrect ID is entered. I put asterisks by the part I'm having problems with. Thank you!

from Tkinter import *
import tkFont
import tkMessageBox

students = {}

class studentDB :
    def __init__(self) :  # Constructor
        # Sets attributes "studentID", "lastName", "firstName"; sets attribute "scores" as empty list
        global students
        students[1000] = student(1000, 'abc', (92, 95, 97))
        students[1001] = student(1001, 'def', (84, 91, 77))

class student :
    def __init__(self, sid, passwd, scoreList) :  # Constructor
        # Sets attributes "studentID", "lastName", "firstName"; sets attribute "scores" as empty list
        self.studentID = sid
        self.passwd = passwd
        self.scores = scoreList

    def getPassword(self) :  # Returns the student ID
        # Returns attribute "studentID"
        return(self.passwd)

    def computeAverage(self) :  # Computes & returns the total score
        # Computes & returns totalScore of list attribute "scores"
        totalScore = 0
        for val in self.scores :
            totalScore = totalScore + val
        return(totalScore/float(len(self.scores)))

def getGrade(score) :
    if (score >= 90) :
        return 'A'
    elif (score >= 80) :
        return 'B'
    elif (score >= 70) :
        return 'C'
    elif (score >= 60) :
        return 'D'
    else :
        return 'F'      

class myApp :
    def __init__(self, top) :
        # Creates a window with:
        #    Label and text box for "Student's ID";
        #    Button labeled "Get Grade" to get the grade for the student; and
        #    Button labeled "Quit" to quit the application
        # You must write the rest of the myApp constructor, here.
        self.root = top
        self.bframe = Frame(self.root)  # Create a container Frame at the bottom
        self.bframe.pack(side=BOTTOM)
        self.xlabel = Label(self.root, text="Student ID")  # Create Label
        self.xlabel.pack(side=LEFT)
        self.xentry = Entry(self.root, bd=5)  # Create Entry box
        self.xentry.pack(side=LEFT)
        self.xentry.focus_set()  # Set focus in Entry box
        self.xopen = Button(self.root, text="Get Grade", command=self.showGrade) # Create open Button
        self.xopen.pack(side=LEFT)
        self.xquit = Button(self.bframe, text="Quit", command=self.quitit) # Create quit Button
        self.xquit.pack(side=BOTTOM)


    def showGrade(self) :
        # Creates either:
        #    Warning message if SID is not found or
        #    Information messaged with grade
        global students
    # You must write the rest of the showGrade method, here.
        **sid = self.xentry.get()
        if 
        import tkMessageBox**

    def quitit(self) :
        # Handler for Quit button click
        self.root.destroy()
        return
# End of myApp class

studentDB()
top = Tk()
app = myApp(top)
top.mainloop()
D Wilson
  • 1
  • 3
  • You can use the showinfo, showwarning, etc. standard dialogs http://effbot.org/tkinterbook/tkinter-standard-dialogs.htm –  Dec 09 '15 at 23:55

2 Answers2

0

Below is python 3 which I use. It should give you the idea. I believe the code is very similar in python 2 with a few minor changes.

# import the messagebox dialogs
from tkinter import messagebox

# these will be in the showGrade method
# to show the sid and grade in a message box
messagebox.showinfo('Student Results', 'SID:{0} Grade:{1}'.format(sid, grade))

# to show that sid not found
messagebox.showerror('Error', 'SID:{0} not found'.format(sid))

The messagebox methods show a popup window and the user clicks 'OK' button

Steve Misuta
  • 1,033
  • 7
  • 7
-2

You could try creating a new GUI when the button is pressed, see:

def showGrade(self) :
    # Creates either:
    #    Warning message if SID is not found or
    #    Information messaged with grade
    global students

    sid = self.xentry.get() #gets entered studentID

    ##CREATE GUI##
    gradewindow = Toplevel()

    gradelbl = Label(gradewindow)
    gradelbl.pack()

    ##END CREATION##

    try:
            grade = #get grade associated with entered studentID
    except ValueError:
            gradelbl.config(text="Student doesn't exist!")
    else:
            gradelbl.config(text=grade)

    gradewindow.update()

Note how you yourself will have to retreive the associated grade, I'm not experienced with dictionary structures, so I'll leave it up to you.

Heres how it works:

  1. The sid value is retrieved, which the user has entered.
  2. The new GUI is defined and created
  3. Instantly after this, the program checks the fetched grade, if it does not exist within the dictionary, it will return a ValueError, which will be excepted and an error will be displayed within the new window.
  4. If it is found however, then the GUI will display this instead.

Hope this helped.

Constantly Confused
  • 595
  • 4
  • 10
  • 24
  • 2
    That would create 2 Tk() instances which can lead to problems –  Dec 10 '15 at 00:04
  • Creating a new root object with its own `mainloop()` is an easy way to make a mess. – TigerhawkT3 Dec 10 '15 at 00:13
  • Oh, what problems? I use this method all the time, am I missing something? – Constantly Confused Dec 10 '15 at 00:26
  • [Here](http://stackoverflow.com/questions/14336472/how-to-create-new-tkinter-window-after-mainloop), [here](http://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop), [here](http://stackoverflow.com/questions/9640013/how-does-creating-two-instances-of-tk-work-with-one-mainloop), [here](http://www.gossamer-threads.com/lists/python/dev/731789)... – TigerhawkT3 Dec 10 '15 at 00:46
  • If you simply change `Tk` to `Toplevel` and remove the call to `mainloop`, your answer would be fine. You can have multiple windows, you just shouldn't have multiple _root_ windows, or multiple `mainloop`s running. – Bryan Oakley Dec 10 '15 at 02:16
  • Edited, guess it's okay now. – Constantly Confused Dec 10 '15 at 07:08