A
First, be aware that you should only have a main window and a single mainloop by thread. For your secondary window you should use tkinter.Toplevel.On the other hand, the code in your secondary window to be in another module and to be able to pass the necessary data, needs to be enclosed in a function you can call from the main module. If you define it at the module level will be executed when importing.It could be something like that:secondary.py:import tkinter as tk
import functools
def simular(parent, vent, modo, est):
top = tk.Toplevel(parent)
top.title("Simulación")
top.geometry("400x200+0+0")
info = tk.Label(top, text="Ventana: {}\nModo: {}\nEstrategia: {}".format(vent, modo, est))
info.pack()
top.protocol("WM_DELETE_WINDOW", functools.partial(volver, parent, top))
btn_volver = tk.Button(top,text="Volver", command=functools.partial(volver, parent, top))
btn_volver.pack()
parent.withdraw()
def volver(parent, top):
parent.deiconify()
top.destroy()
and in your main module:import secundaria
def valoresSimular(*args):
secundaria.simular(main, varDes.get(), varModo.get(), varEst.get())
The method withdraw() allows us to temporarily hide the main window while deiconify() He shows it again. For his part, top.protocol("WM_DELETE_WINDOW", func) allows to call a certain function when pressing the button X of the daughter window, in this case it acts as when pressing the button return.I know you don't want to use classes, but it's definitely a better way to structure your code and make it more reusable. I'll leave you a version using POO with some more changes. As a note, use wildcard to import (from tkinter import *) is usually a bad practice and should be avoided.secondary.py:import tkinter as tk
class VentanaSimulacion(tk.Toplevel):
def init(self, parent, vent, modo, est, *args, **kwargs):
super().init(parent, *args, **kwargs)
self.parent = parent
self.title("Simulación")
self.geometry("400x200+0+0")
self.protocol("WM_DELETE_WINDOW", self.volver)
info = tk.Label(self, text="Ventana: {}\nModo: {}\nEstrategia: {}".format(vent, modo, est))
info.pack()
tk.Button(self, text="Volver", command=self.volver).pack()
self.parent.withdraw()
def volver(self):
self.parent.deiconify()
self.destroy()
main.py:import tkinter as tk
import secundaria
class MainWindow(tk.Frame):
def init(self, parent, *args,**kwargs):
super().init(parent, *args, **kwargs)
self.parent = parent
self.parent.title("Configuración")
self.configure(background='light grey') # Color de Fondo
# Variables
self.var_des = tk.StringVar(self)
self.var_des.set('Seleccionar...')
self.var_modo = tk.StringVar(self)
self.var_modo.set('Seleccionar...')
self.var_est = tk.StringVar(self)
self.var_est.set('Seleccionar...')
# Caja texto
label = tk.Label(self, bg="light grey", text='Ventana Deslizante',
padx=30, pady=5, width=20
)
label.grid(row=0, column=0)
label = tk.Label(self, bg="light grey", text='Modo de Transmisión',
padx=30, pady=5, width=20
)
label.grid(row=1, column=0)
label = tk.Label(self, bg="light grey", text='Estrategia de Transmisión',
padx=30, pady=5, width=20
)
label.grid(row=2, column=0)
# Caja de Opciones
opciones = ['1','2', '3', '4', '5', '6', '7']
menu = tk.OptionMenu(self, self.var_des, *opciones)
menu.config(width=20)
menu.grid(row = 0, column = 1, padx = 30, pady = 30)
opciones = ['NRM','ABM']
menu = tk.OptionMenu(self, self.var_modo, *opciones)
menu.config(width=20)
menu.grid(row = 1, column = 1, padx = 30, pady = 30)
menu = ['GoBack-N','Repetición Selectiva']
menu = tk.OptionMenu(self, self.var_est, *opciones)
menu.config(width=20)
menu.grid(row = 2, column = 1, padx = 30, pady = 30)
# Botones Limpiar y Simular
boton = tk.Button(self, text="Limpiar", width=20, command=self.valores_limpiar)
boton.grid(row=3, column=0, padx=20, pady=30)
boton = tk.Button(self, text="Simular", width=20, command=self.valores_simular)
boton.grid(row=3, column=1, padx=20, pady=30)
def valores_limpiar(self):
self.var_des.set('Seleccionar...')
self.var_modo.set('Seleccionar...')
self.var_est.set('Seleccionar...')
def valores_simular(self):
secundaria.VentanaSimulacion(self.parent,
self.var_des.get(),
self.var_modo.get(),
self.var_est.get()
)
if name == "main":
root = tk.Tk()
MainWindow(root).pack(side="top", fill="both", expand=True)
root.mainloop()