Ciao ragazzi. Sto prendendo la mano nel fare le interfacce grafiche. Ora a parte la barra di caricamento che ho chiesto aiuto online, sto continuando a lavorare. Sto riscontrando dei problemi perché, quando vado a cliccare, nel menù geometria, la voce calcolo del determinante, questo errore:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\fabio\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\Users\fabio\Desktop\Intelligenza.py", line 133, in suboption1
determinant_window = tk.Toplevel(self.root)
File "C:\Users\fabio\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2650, in __init__
BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
File "C:\Users\fabio\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2601, in __init__
self.tk.call(
_tkinter.TclError: can't invoke "toplevel" command: application has been destroyed
di seguito metto il mio codice
import tkinter as tk
from tkinter import ttk, filedialog
import numpy as np
import time
import threading
class CalculatorApp:
def __init__(self, root):
self.root = root
self.root.title("Calcolatrice")
self.create_ui()
def create_ui(self):
self.entry = ttk.Entry(self.root, font=("Helvetica", 20))
self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
buttons = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
('0', 4, 0), ('.', 4, 1), ('=', 4, 2), ('+', 4, 3),
]
for (text, row, column) in buttons:
ttk.Button(self.root, text=text, command=lambda t=text: self.on_button_click(t)).grid(row=row, column=column, padx=5, pady=5)
def on_button_click(self, value):
if value == "=":
try:
result = eval(self.entry.get())
self.entry.delete(0, tk.END)
self.entry.insert(0, str(result))
except Exception as e:
self.entry.delete(0, tk.END)
self.entry.insert(0, "Errore")
else:
self.entry.insert(tk.END, value)
class LoadingApp:
def __init__(self, root):
self.root = root
self.root.title("Caricamento")
self.progress_var = tk.IntVar()
self.progress_bar = ttk.Progressbar(self.root, variable=self.progress_var, maximum=100)
self.progress_bar.pack(pady=10)
self.center_window(self.root)
self.label = tk.Label(self.root, text="Caricamento in corso...")
self.label.pack()
self.start_loading()
def center_window(self, win):
screen_width = win.winfo_screenwidth()
screen_height = win.winfo_screenheight()
win_width = win.winfo_reqwidth()
win_height = win.winfo_reqheight()
x = int((screen_width - win_width) / 2)
y = int((screen_height - win_height) / 2)
win.geometry(f"{win_width}x{win_height}+{x}+{y}")
def simulate_loading(self):
for i in range(101):
self.progress_var.set(i)
self.label.config(text=f"Caricamento in corso... {i}%")
self.root.update_idletasks()
time.sleep(0.1)
self.root.after(0, self.open_home)
def start_loading(self):
loading_thread = threading.Thread(target=self.simulate_loading)
loading_thread.start()
def open_home(self):
self.root.destroy()
home_window = tk.Tk()
home_window.title("Home")
menu = tk.Menu(home_window)
home_window.config(menu=menu)
options_menu = tk.Menu(menu)
menu.add_cascade(label="Opzioni", menu=options_menu)
options_menu.add_command(label="Opzione 1", command=self.option1)
options_menu.add_command(label="Opzione 2", command=self.option2)
options_menu.add_command(label="Opzione 3", command=self.option3)
options_menu.add_command(label="Opzione 4", command=self.option4)
submenu = tk.Menu(menu)
menu.add_cascade(label="Geometria", menu=submenu)
submenu.add_command(label="Calcolo del determinante", command=self.suboption1)
submenu.add_command(label="Calcolo del rango", command=self.suboption2)
submenu.add_command(label="Lineare indipendenza",command=self.suboption3)
submenu = tk.Menu(menu)
menu.add_cascade(label="Analisi", menu=submenu)
submenu.add_command(label="Integrali", command=self.suboption1)
submenu.add_command(label="Derivate", command=self.suboption2)
submenu = tk.Menu(menu)
menu.add_cascade(label="Fisica", menu=submenu)
submenu.add_command(label="Calcolo vettoriale", command=self.suboption1)
submenu.add_command(label="Statica", command=self.suboption2)
calculator = CalculatorApp(home_window)
home_window.mainloop()
def option1(self):
print("Opzione 1 selezionata")
def option2(self):
print("Opzione 2 selezionata")
def option3(self):
print("Opzione 3 selezionata")
def option4(self):
print("Opzione 4 selezionata")
def open_file_and_calculate_determinant(self):
file_path = filedialog.askopenfilename(filetypes=[("File di testo", "*.txt"), ("Tutti i file", "*.*")])
if file_path:
try:
with open(file_path, 'r') as file:
matrix_content = file.read()
matrix = np.array(eval(matrix_content))
determinant = np.linalg.det(matrix)
result_str = f"Il determinante della matrice è: {determinant}"
tk.messagebox.showinfo("Risultato", result_str)
except Exception as e:
tk.messagebox.showerror("Errore", "Si è verificato un errore durante il calcolo del determinante.")
def suboption1(self):
determinant_window = tk.Toplevel(self.root)
determinant_window.title("Calcolo del Determinante")
label = tk.Label(determinant_window, text="Seleziona un file contenente la matrice:")
label.pack(pady=10)
load_file_button = ttk.Button(determinant_window, text="Carica da File e Calcola", command=self.open_file_and_calculate_determinant)
load_file_button.pack(pady=10)
def suboption2(self):
print("Sottopzione 2 selezionata")
def suboption3(self):
print("Sottopzione 3 selezionata")
def main():
root = tk.Tk()
app = LoadingApp(root)
root.mainloop()
if __name__ == "__main__":
main()