Я создал программу tkinter для расчета gpa, единственная часть, которую я оставил, это часть обработки ошибок, и я просто не смог найти способ написать бесконечный цикл обработки ошибок. Я пытаюсь написать цикл, поэтому, когда вы задаете другое значение для числа классов, отличного от (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()