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)