Python Forum
[Tkinter] create and insert a new frame on top of another frame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] create and insert a new frame on top of another frame
#1
I have an idea and I want to realize it on my little project in tkinter with python 3.6
I'd like to be told how I'm going to start doing this

I want to create a frame or canvas, and when I click on a button "add_sub_frame": a sub frame is created and inserted into the parent frame.
Then, when I click again on the button "add_sub_frame", another subframe is created and added above the subframe previously created.
and this operation is repeated each time I click on the button "add_sub_frame"
at the end, I would have as result an infinity of sub frames that add one above the other in the frame or canvas parent

I thought about: Canvas.create_window (x, y, option, ...)
I would like to have some help
thank you
Reply
#2
I struggle to understand why anybody would want to write something like this, but it should be fairly straightforward. It's even possible to give each Frame a random color. Here's how I would imagine somebody doing it:
from tkinter import *
import random

def randomColor ():
    randomRed = ("00" + random.randint(0, 255))[-2]
    randomGreen = ("00" + random.randint(0, 255))[-2]
    randomBlue = ("00" + random.randint(0, 255))[-2]
    return "#{}{}{}".format(randomRed, randomGreen, randomBlue)

class RandomColorNestedFramesApp:

    def __init__(self, master):
        self.master = master
        self.master.geometry("1024x1024+50+50")
        self.bgFrame = Frame(self.master, bg = randomColor())
        self.addFrameButton = Button(self.bgFrame, text = "add sub-frame", bg = "#FF00CC", fg = "black", font = "Times 11", command = self.addDaughterFrame)
        self.addFrameButton.place(relx = 0, rely = 0, relwidth = 0.25, relheight = 0.07)
        self.frameList = []

    def addDaughterFrame (self):
        if self.frameList == []:
            self.frameList.append(Frame(self.bgFrame, bg = randomColor()))

        else:
            self.frameList.append(Frame(self.frameList[-2], bg = randomColor()))

        self.frameList[-1].place(relx = 0.2, rely = 0.2, relwidth = 0.96, relheight = 0.96)

if __name__ == "__main__":
    root = Tk()
    theApp = RandomColorNestedFramesApp(root)
    root.mainloop()
Give it a try and let me know how it works. We definitely can't sell this to Microsoft for millions of dollars but I had fun writing it.

That first attempt was rather hastily thrown together, bad random color function included. This code actually works:
from tkinter import *
import random
 
def randomColor ():
    randomRed = ("00" + hex(random.randint(0, 255))[2:])[-2]
    randomGreen = ("00" + hex(random.randint(0, 255))[2:])[-2]
    randomBlue = ("00" + hex(random.randint(0, 255))[2:])[-2]
    return "#{}{}{}".format(randomRed, randomGreen, randomBlue)
 
class RandomColorNestedFramesApp:
 
    def __init__(self, master):
        self.master = master
        self.master.geometry("1024x1024+50+50")
        self.bgFrame = Frame(self.master, bg = randomColor())
        self.bgFrame.place(relx = 0, rely = 0, relwidth = 1, relheight = 1)
        self.addFrameButton = Button(self.bgFrame, text = "add sub-frame", bg = "#FF00CC", fg = "black", font = "Times 11", command = self.addDaughterFrame)
        self.addFrameButton.place(relx = 0, rely = 0, relwidth = 0.25, relheight = 0.07)
        self.frameList = []
 
    def addDaughterFrame (self):
        if len(self.frameList) < 2:
            self.frameList.append(Frame(self.bgFrame, bg = randomColor()))
 
        else:
            self.frameList.append(Frame(self.frameList[-2], bg = randomColor()))
 
        self.frameList[-1].place(anchor = "center", relx = 0.5, rely = 0.5, relwidth = 0.96, relheight = 0.96)
 
if __name__ == "__main__":
    root = Tk()
    theApp = RandomColorNestedFramesApp(root)
    root.mainloop()
Reply
#3
I have actually used this technique (and It was a perfect fit at the time) if you make sure that the sizes and placement are the same, and you use tkinter lift and lower you can actually create a very pleasing effect.
Reply
#4
Hi keames

Here a little snippet to play with:
import tkinter as tk
import random

APP_TITLE = "Frame Stack"
APP_XPOS = 50
APP_YPOS = 50
APP_WIDTH = 800
APP_HEIGHT = 800


class Application(object):

    def __init__(self, main_win):
        self.main_win = main_win
        
        self.build()
        
    def build(self):
        self.frame_stack = list()
        self.stack_pointer = 0
        
        self.main_frame = tk.Frame(self.main_win)
        self.main_frame.pack(fill='both', expand=True)
        self.main_frame.columnconfigure(0, weight=1)
        self.main_frame.rowconfigure(0, weight=1)
        
        button_frame = tk.Frame(self.main_frame)
        button_frame.grid(row=1, column=0, pady=4)

        self.stack_level_var = tk.StringVar()
        tk.Label(button_frame, bg='white', width=4, relief='sunken', bd=1,
            textvariable=self.stack_level_var
            ).pack(side='left', padx=10)
            
        tk.Button(button_frame, text="Up",
            command=self.button_up).pack(side='left')
        tk.Button(button_frame, text="Down",
            command=self.button_down).pack(side='left', padx=(2, 20))
        tk.Button(button_frame, text="Add",
            command=self.add_frame).pack(side='left')
        tk.Button(button_frame, text="Exit",
            command=self.main_win.destroy).pack(side='left')
        
        self.add_frame()
        
    def add_frame(self):
        level_frame = tk.Frame(self.main_frame, bg=self.random_color())
        level_frame.grid(row=0, column=0, sticky='news')
        level_frame.lift()
        self.frame_stack.append(level_frame)
        self.stack_pointer = len(self.frame_stack)-1
        self.stack_level_var.set(self.stack_pointer)  
        #self.show_level_frame()
        #self.stack_pointer += 1

    def button_up(self):
        self.stack_navigator('up')

    def button_down(self):
        self.stack_navigator('down')
    
    def stack_navigator(self, direction):
        if direction == 'up':
            if self.stack_pointer < len(self.frame_stack)-1:
                self.stack_pointer += 1
        elif direction == 'down':
            if self.stack_pointer > 0:
                self.stack_pointer -= 1
        
        self.show_level_frame()       
        print(self.stack_pointer, len(self.frame_stack), direction)   
    
    def show_level_frame(self):
                 
        self.stack_level_var.set(self.stack_pointer)        
        self.frame_stack[self.stack_pointer].lift()
                        
    def random_color (self):
        random_red = ("00" + hex(random.randint(0, 255))[2:])[-2]
        random_green = ("00" + hex(random.randint(0, 255))[2:])[-2]
        random_blue = ("00" + hex(random.randint(0, 255))[2:])[-2]
        return "#{}{}{}".format(random_red, random_green, random_blue)

           
def main():
    main_win = tk.Tk()
    main_win.title(APP_TITLE)
    main_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    main_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    
    app = Application(main_win)
    
    main_win.mainloop()
 
 
if __name__ == '__main__':
    main()      
:-)
Reply
#5
thanks for all information
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Horizontal extension of widgets + frame size adapted to content Fab117 3 480 Feb-22-2024, 06:54 PM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 996 Jan-16-2024, 06:44 PM
Last Post: rob101
  Problem passing argument to functionin inTkinter frame ericwm7248 3 1,018 Sep-11-2023, 03:11 PM
Last Post: deanhystad
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,580 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  tkinter mapview in a frame janeik 2 1,325 Jun-22-2023, 02:53 PM
Last Post: deanhystad
  [Tkinter] Image in Frame in Tabbed Widget Columbo 4 2,149 Sep-28-2022, 08:04 PM
Last Post: deanhystad
  [Tkinter] Frame with treeview virencm 5 7,004 Feb-01-2022, 04:58 PM
Last Post: deanhystad
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 4,134 Sep-30-2021, 05:57 PM
Last Post: menator01
  [Tkinter] How to create scrollable frame mandiatutti 7 4,453 Aug-07-2021, 03:34 PM
Last Post: deanhystad
  [Tkinter] using different frame for some widgets rwahdan 1 2,378 Jul-13-2021, 08:32 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020