python - 在 tkinter 中设置错误处理循环
问题描述
我创建了一个用于计算 gpa 的 tkinter 程序,我剩下的唯一部分是错误处理部分,我只是找不到编写无限错误处理循环的方法。我正在尝试编写循环,因此当您为 (1,2,3,4,5,6,7,8) 以外的类数提供不同的值时,程序不会继续,直到您提供类数正确的价值。
from tkinter import *
import math
master = Tk()
master.title('Gpa Calculator')
master.geometry("400x400")
master.resizable(width=False, height=False) #Makes the window size fixed, so you can't make it bigger or smaller by hand.
def Print_Entry():
global print_command
global Entry_int
Entry = n_of_classes_entry.get()
Entry_int = int(Entry)
Create_Classes()
#A function to destroy and recreate the program
def restart():
classentries.clear()
classintegers.clear()
classintegers_gpa.clear()
list = master.grid_slaves()
for l in list:
l.destroy()
start_program()
textlist = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eight']
classentries = []
classintegers = []
classintegers_gpa = []
#Creates the entry, text, and entry boxes for the classes
def Create_Classes():
for i in range (0, Entry_int):
classentries.append(Entry(master))
for i in range (0, Entry_int):
classentries[i].grid(row=1 + i, column=1)
for i in range (0, Entry_int):
Label(master, text=textlist[i] + ' grade:').grid(row=i + 1, sticky=E)
calculate_button = Button(master, bg="red", text="Calculate", command=Calculate).grid(row=9, column=1, sticky=W, pady=4)
def Calculate():
for i in range (0, Entry_int):
classintegers.append(round(float(classentries[i].get())))
if classintegers[i] in range(0,50):
classintegers[i] = 0
if classintegers[i] in range(50,55):
classintegers[i] = 1.0
if classintegers[i] in range(55,60):
classintegers[i] = 2.0
if classintegers[i] in range(60,65):
classintegers[i] = 2.3
if classintegers[i] in range(65,70):
classintegers[i] = 2.7
if classintegers[i] in range(70,75):
classintegers[i] = 3.0
if classintegers[i] in range(75,80):
classintegers[i] = 3.3
if classintegers[i] in range(80,85):
classintegers[i] = 3.7
if classintegers[i] in range(85,110):
classintegers[i] = 4.0
classintegers_gpa.append(classintegers[i])
last_calculate = True
# This while loop is for the program to print the gpa_calculation once.
if last_calculate == True:
global gpa_calculation
gpa_calculation = round(sum(classintegers_gpa))/len(classintegers_gpa)
gpa_rounded = float("{0:.2f}".format(gpa_calculation))
printgpa = Label(master, text="Your Gpa is: " + str(gpa_rounded)).grid(row=10,column=1)
Button(master,text='Restart',command=restart).grid(row=11)
Print_Entry()
def start_program():
global n_of_classes
global n_of_classes_entry
global n_of_classes_button
n_of_classes = Label(master, text="Number of Classes 1-8:").grid(row=0, sticky=E)
n_of_classes_entry = Entry(master)
n_of_classes_entry.grid(row=0, column=1)
n_of_classes_button = Button(master, bg="green", command=Print_Entry,height=1, width=2).grid(row=0, column=2)
start_program()
解决方案
我建议在 IF 语句中包含 print_entry 定义的内容。如果该值是可接受的,则继续,否则发送错误消息。您可以使用 tkinter showinfo 消息框发送错误。请参阅下面的代码建议:
from tkinter import messagebox
def Print_Entry():
nb_entry = n_of_entry.get()
if nb_entry <9 and nb_entry > 0 :
global print_command
global Entry_int
Entry = n_of_classes_entry.get()
Entry_int = int(Entry)
Create_Classes()
else :
messagebox.showinfo("Error", "The number of classes must be between 1 and 8")
推荐阅读
- c# - Razor Pages 将处理程序中的图像返回给 ` ` 元素
- oauth-2.0 - OAuth 错误 400:invalid_request 缺少方案:/login
- react-native - 无法为 react-native 17.0.1 安装 material-ui/core
- excel - 有没有办法使用每个基于文本的单元格作为分隔符将一列分成多列?
- gpu - 在 OpenCL 上使用阻塞写入和带有非阻塞写入的 event.wait() 之间的区别
- ios - AF.upload 问题“模式无法匹配 URLRequest 类型的值”
- ag-grid - ag-Grid: enableRowGroup 只在 ag-Grid-Enterprise 中有效,你的列定义不应该有 enableRowGroup
- c# - 无法转换类型'System.Threading.Tasks.Task
' 到 'Microsoft.AspNetCore.Mvc.OkObjectResult' - opencv - 为动画目的修复 Matplotlib xlim 和 ylim 重绘
- android - 为什么我的 Webview 在 kotlin 片段中显示空白?