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
#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


Messages In This Thread
RE: create and insert a new frame on top of another frame - by keames - Apr-16-2019, 01:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Horizontal extension of widgets + frame size adapted to content Fab117 3 625 Feb-22-2024, 06:54 PM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 1,254 Jan-16-2024, 06:44 PM
Last Post: rob101
  Problem passing argument to functionin inTkinter frame ericwm7248 3 1,105 Sep-11-2023, 03:11 PM
Last Post: deanhystad
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,700 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  tkinter mapview in a frame janeik 2 1,422 Jun-22-2023, 02:53 PM
Last Post: deanhystad
  [Tkinter] Image in Frame in Tabbed Widget Columbo 4 2,259 Sep-28-2022, 08:04 PM
Last Post: deanhystad
  [Tkinter] Frame with treeview virencm 5 7,199 Feb-01-2022, 04:58 PM
Last Post: deanhystad
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 4,271 Sep-30-2021, 05:57 PM
Last Post: menator01
  [Tkinter] How to create scrollable frame mandiatutti 7 4,669 Aug-07-2021, 03:34 PM
Last Post: deanhystad
  [Tkinter] using different frame for some widgets rwahdan 1 2,436 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