Reputation: 63
I'm using python to make a small app (for personal use/training) and I'm having trouble understanding how the frame module from tkinter works. All I have done so far is tried to place colored squares to "map" the window visually but I'm already having problems.
Problem is, whenever I try to "print" the blue frame, the yellow one doesn't.
import tkinter
class leftFrame(tkinter.Frame):
def __init__(self, parent):
self.frame = tkinter.Frame(parent, width=450, height=400, bg="blue")
self.parent = parent
self.initialize()
def initialize(self):
self.frame.grid()
#if I comment this previous line, the yellow frame will be printed
#pass
class mainFrame(tkinter.Frame):
def __init__(self, master=None):
self.frame = tkinter.Frame(master, width=600, height=400, bg="yellow")
self.master = master
self.initialize()
def initialize(self):
self.frame.grid()
self.left = leftFrame(self.frame)
root = tkinter.Tk()
root.geometry("600x400")
root.wm_title("Tkinter test")
root.configure(bg="green")
#root.resizable(False, False)
app = mainFrame(root)
root.mainloop()
I've used classes to handle my "layout objects" as it seemed a cleaner way to do so, but as I am new to python & object programming, so chances are I did something wrong there, if someone cares to explain me why this bit of code isn't working it would be a great help! Thanks ;)
Upvotes: 3
Views: 500
Reputation: 36662
The background of your root ends up covered by the two frames you are inserting.
I suggest the following:
-> a placeholder label was inserted to offset the inner frames, so you can see the background color of the root.
import tkinter as tk
class InnerFrame(tk.Frame):
"""A tk.Frame to be inserted in a container
"""
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs)
self.master = master
class MainFrame(tk.Frame):
"""Container for two InnerFrame
"""
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.left_frame = InnerFrame(self, width=450, height=400, bg="blue")
self.left_frame.grid(row=0, column=0)
self.right_frame = InnerFrame(self, width=450, height=400, bg="green")
self.right_frame.grid(row=0, column=1)
class App(tk.Tk): # NOTE: not a tk.Frame
"""A tk.root that contains the entire GUI app
"""
def __init__(self, title, color, width, height):
super().__init__()
self.title(title)
self.configure(bg=color)
self.geometry(f'{width}x{height}')
self.placeholder_label = tk.Label(self, text='placeholder')
self.placeholder_label.grid(row=1, column=0)
self.mainframe = MainFrame(self)
self.mainframe.grid()
# create and launch
App(title='Tkinter test', color='yellow', width=900, height=400).mainloop()
Upvotes: 1