Layering graphical interfaces on top of each other

main.py

import tkinter as tk
import Mod1
import Mod2

root = tk.Tk()
m1 = Mod1.Model(root)
m2 = Mod2.Model(root)
m1.grid(column= 0, row=0)
m2.grid(column = 0 ,row=0)

def callback():
    m1.lift()    
b = tk.Button(text="click me", command=callback)
b.grid(column=0,row=1)

Mod1.py

from __main__ import tk

class Model(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master
        self.configure(bg="red", width=300, height=300)

Mod2.py

from __main__ import tk

class Model(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master
        self.configure(bg="blue", width=300, height=300)

Explaination

In your Modules you define your Model’s as a subclass of tk.Frame, that gives you the ability to handle your Models as same as a tk.Frame.

Leave a Comment