Superposición de ventanas al crear switch en Tkinter Python

58 Views Asked by At

I am trying to create a switch style to be able to access a frame, when running main_screen.py it appears at the top of products.

When we click here we get it leads to a derivatives button but if we click on derivatives a window is superimposed where products appear again. If I immediately click again on the derivatives button the window is eliminated and the program works normally. I have tried everything and I have not been able to solve it.

Where I have the root is the file sumamatrices.py:

from tkinter import *

raiz = Tk()
raiz.title("Math IV")
raiz.resizable(1, 1)
raiz.iconbitmap("LOGO MATH PLUS.ico")
raiz.tk_setPalette(background='#edf2f4')

# Obtiene las dimensiones de la pantalla
screen_width = raiz.winfo_screenwidth()
screen_height = raiz.winfo_screenheight()

# Establece el tamaño de la ventana al tamaño de la pantalla
raiz.geometry(f'{screen_width}x{screen_height}+0+0')

# Evita que la ventana se ajuste automáticamente a su contenido
raiz.pack_propagate(False)

where the derivatives thing is in derivatives.py:

from tkinter import *
import matplotlib.figure as fig
import matplotlib
import sympy as sp
from tkinter import messagebox
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import sympy.plotting.plot as symplot


def derive(expr):
    return sp.latex(sp.diff(expr))

def graph():
    global i

    i = funcion.get()
    
    tmptext = derive(i)
    tmptext = "$ \\frac{dy}{dx}" + sp.latex(sp.sympify(i)) + "=" + tmptext + "$"

    ax.clear()
    if 120 <= len(tmptext) <= 122:
        fs = 12
    elif 122 < len(tmptext) <= 140:
        fs = 11
    else:
        fs = 16

    ax.text(0.05, .4, tmptext, fontsize=fs)
    canvas.draw()

    symplot(sp.diff(i))

def verificar_campos():
    if len(funcion.get())==0:
        messagebox.showerror("Error", "Por favor, introduce una función válida.")
    else:
        graph()




def botonpulsado16():
    from main_screen import raiz
    from main_screen import options_fm, tools_page

    global funcion
    global etiqueta
    global canvas
    global ax


    for widget in raiz.winfo_children():
        if widget != options_fm:
            widget.destroy()

    miframe5=Frame(raiz)
    miframe5.pack(expand=True)

    label=Label(miframe5, text="Derivada Simbólica", fg='white', font=("arial black", 30))
    label.pack(pady=(0,32))

    anuncio = Label(miframe5, text="Introduce una función de x:", font=("Arial", 15), fg="blue")
    anuncio.pack()
    funcion = Entry(miframe5, font=("Arial", 15), bg='white')
    funcion.pack()
    etiqueta = Label(miframe5, text="Resultado", font=("Arial", 15), fg="red")
    etiqueta.pack()
    boton1 = Button(miframe5, text="Derivar Función", font=("Arial", 15), command=verificar_campos)
    boton1.pack()
    botonback=Button(miframe5, text="Atras", font=("Arial", 15), command=tools_page)
    botonback.pack()

    fig = matplotlib.figure.Figure(figsize=(10, 1), dpi=80)
    ax = fig.add_subplot(111)

    canvas = FigureCanvasTkAgg(fig, master=etiqueta)
    canvas.get_tk_widget().pack(side="top", fill="both", expand=True)
    canvas._tkcanvas.pack(side="top", fill="both", expand=True)

    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    try:
        graph()
    except Exception as e:
        print("Error:", e)

and the main file is main_screen.py:


from sumamatrices import raiz
import tkinter as tk

from derivadas import botonpulsado16

options_fm = tk.Frame(raiz)


product_btn = tk.Button(options_fm, text='Products', font=('Arial', 13),
                        bd=0, fg='#0097e8', activeforeground='#0097e8',
                        command=lambda:switch(indicator_lb=product_indicator_lb,
                                              page=tools_page))
product_btn.place(x=125, y=10, width=125)
product_indicator_lb = tk.Label(options_fm)
product_indicator_lb.place(x=147, y=40, width=80, height=2)


options_fm.pack(pady=6, fill=tk.X)

options_fm.configure(height=50)


def switch(indicator_lb, page):

    for child in options_fm.winfo_children():
        if isinstance(child, tk.Label):
            child['bg'] = 'SystemButtonFace'
    indicator_lb['bg'] = '#0097e8'

    for widget in raiz.winfo_children():
        if widget != options_fm:
            widget.destroy()
            raiz.update()

    page()

def tools_page():

    for widget in raiz.winfo_children():
        if widget != options_fm:
            widget.destroy()


    # Configura el framet para expandirse
    framet = tk.Frame(raiz, highlightthickness=0, highlightbackground='#78c0e0')
    framet.pack(fill='both')

    ml=tk.Label(framet, text="Tools", fg="#124559", font=("Poppins Medium", 60))
    ml.grid(row=0, column=0, padx=10, pady=10, columnspan=6)

    ml2=tk.Label(framet, text="Elija una opción: ", fg="#124559",
            font=("Poppins Medium", 18))
    ml2.grid(row=1, column=0, padx=10, pady=10, columnspan=6)


    boton4=tk.Button(framet, text="Derivadas", width="20",font=("Poppins Medium", 15),fg='white',bg='#2a7097', bd=1, command= botonpulsado16)
    boton4.grid(row=2, column=3, sticky="we", padx=10, pady=30)

raiz.mainloop()  
0

There are 0 best solutions below