-1

Conceptually, if I define a Frame and create it using, say, grid, then within Frame can I use any of the geometry managers? Below is a MWE. My problem is I am trying to write a GUI where the base layout is with grid, but within those widgets I am having loads of trouble setting objects. I think it may be because I am passing through modules. I am aware of this answer here, and it sheds some light, but if I pass a tk Frame object to a class, do a bunch of Tk stuff to it, how is it being handled in terms of geometry management?

Specifically, say I have a base gui layout:

import Tkinter as tk
from Tkinter import Frame
from modules.subfolder.name_of_file import SomeClass

class Window(tk.Frame):
    def __init__(self, master):
        self.master = master
        self.configure_gui()
        self.create_widgets()
        self.create_modules()

    def configure_gui(self):
        self.master.wm_attributes('-fullscreen','true')
        self.master.configure(background='black')

        # in pixels
        self.screen_width = self.master.winfo_screenwidth()
        self.screen_height = self.master.winfo_screenheight()

        # max height for header and footer
        self.foot_height = 100
        self.header_above_height = 100
        self.header_below_height = 100

    def create_widgets(self):

        # Main layout - like a document, header footer, main
        self.header_above = Frame(self.master, bg='black', width = self.screen_width, height=self.header_above_height)
        self.header_below = Frame(self.master, bg='black', width = self.screen_width, height=self.header_below_height)
        self.center = Frame(self.master, bg='black', width=self.screen_width)
        self.footer = Frame(self.master, bg='black', width = self.screen_width, height = self.foot_height)

        # this makes row 1 grow first so it will push out
        # to the top and bottom
        self.master.grid_rowconfigure(2, weight=1)
        self.master.grid_columnconfigure(1, weight=1)

        self.header_above.grid(row=0, sticky="ew")
        self.header_below.grid(row=1, sticky="ew")
        self.center.grid(row=2, sticky="nsew")
        self.footer.grid(row=3, sticky="ew")

    def create_modules(self):

        # Module
        self.sub_widget = SomeClass(self.header_above)
        self.sub_widget.pack()
def main():
   root = tk.Tk()
   gui = Window(root)
   root.mainloop()

if __name__ == '__main__':
    main()

Often, I am not getting the expected behavior in the self.sub_widget.pack(). Let's say I wanted to pack in two Labels and have them right adjusted, so against the right side of the screen back in its parent frame header_above. How can I acheive this. Because now it comes back left-adjusted.

from Tkinter import *
from PIL import Image, ImageTk

class SomeClass(Frame):
    def __init__(self, parent, *args, **kwargs):
        Frame.__init__(self, parent, bg='black')

        self.config(bg='black')

        image = Image.open('images/Moon.png')
        image = image.resize((100, 100), Image.ANTIALIAS)
        image = image.convert('RGB')
        photo = ImageTk.PhotoImage(image)

        self.display_icon = Label(self, image=photo, width=100, height=100)
        self.display_icon.pack(side=RIGHT)

        self.display_name = Label(self,text="name to display",bg='black',fg="white")
        self.display_name.pack(side=TOP)
superhero
  • 185
  • 3
  • 16
  • 1
    It would help if you gave each frame distinct colors while debugging this. Otherwise it's impossible to see where one frame ends and the next begins. – Bryan Oakley Jul 02 '18 at 03:32
  • 1
    As for your alignment problem, you need to specify an anchor, as Bryan explains [here](https://stackoverflow.com/a/41770855/4014959). The side arg just tells pack where to start packing from, it doesn't control alignment. – PM 2Ring Jul 02 '18 at 03:40

1 Answers1

0

Conceptually, if I define a Frame and create it using, say, grid, then within Frame can I use any of the geometry managers?

Yes, you can. Every widget can use whatever geometry manager it wants to manage its own children, independent of what the rest of the widgets use.

The fact that are or aren't using modules is totally irrelevant.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I think the OP's real question is about widget alignment. The stuff about using a different layout manager in a child was only mentioned as a possible cause of the widgets not being aligned as expected. – PM 2Ring Jul 02 '18 at 03:49
  • @PM2Ring: it's hard to tell. They didn't provide working code ``. I assumed since he asked a question in the very first sentence, he wanted an answer to that question. – Bryan Oakley Jul 02 '18 at 03:55
  • @BryanOakley Made the code executable. Another behavior I don't understand...in the `sub_widget.pack()` call, if I put `side=RIGHT` I get an error, NameError: global name 'RIGHT' is not defined`. – superhero Jul 02 '18 at 13:40