gpt4 book ai didi

python - 在Tkinter中设置错误处理循环

转载 作者:行者123 更新时间:2023-12-03 08:45:08 25 4
gpt4 key购买 nike

我创建了一个tkinter程序来计算gpa,我剩下的唯一部分是错误处理部分,而我只是找不到写无限错误处理循环的方法。我正在尝试编写循环,所以当您给除(1,2,3,4,5,6,7,8)以外的其他类数以不同的值时,程序将不会继续运行,直到您提供了类数正确的值(value)。

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")

关于python - 在Tkinter中设置错误处理循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56588339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com