[英]How do I make the python GUI display info in a popup window by entering an ID number? And an error message?
这是我到目前为止的代码。 最后,输入学生证后,我很难弄清楚如何制作一个弹出窗口,说“等级为[A]”。 如果输入了错误的ID,我也不知道如何创建一个弹出窗口,显示“找不到学生ID”。 我在遇到问题的部分加上星号。 谢谢!
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()
以下是我使用的python 3。 它应该给你的想法。 我相信代码与python 2非常相似,但有一些小的更改。
# 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))
消息框方法显示一个弹出窗口,用户单击“确定”按钮
您可以尝试在按下按钮时创建新的GUI,请参阅:
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()
请注意,您自己将不得不重新获得相关的成绩,我对字典结构没有经验,所以我会把它留给您。
这是它的工作方式:
sid
值。 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.