Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6711225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:06:52+00:00 2026-05-26T08:06:52+00:00

I am having a problem with either Tkinter’s GRID geometry manager or custom classes

  • 0

I am having a problem with either Tkinter’s GRID geometry manager or custom classes or both. Most likely both. Here’s the code:

import sys
from tkinter import 

def main():
    app = App()
    app.master.title("Sample application")
    app.mainloop()

class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid(sticky=N+S+E+W)

        top = self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        self.rowconfigure(1, weight=1)
        self.columnconfigure(0, weight=1)
        self.__createWidgets()

    def __createWidgets(self):
        self.f1 = Frame(self, height=100, width=200,
            bg='green')
        self.f1.grid(row=0, column=0, sticky=E+W)

        self.f2 = Frame( self, bg= "yellow", height=100, width=200 ) ############################# CHANGE to self.f2 = myTestFrame( self )
        self.f2.grid(row=1, sticky = N+S+E+W)

        self.f3 = Frame( self, bg = "cyan", height = 100, width = 200 )
        self.f3.grid(row=2, sticky = E+W)

        self.quitButton = Button ( self, text="Quit", command=self.quit )
        self.quitButton.grid(row=4, column=0, columnspan=100,
            sticky=E+W)

class myTestFrame( Frame ):
    def __init__( self, parent):
        Frame.__init__(self, None)

        self.myText = Text( self )
        self.myText.grid()

        #self.testFrame = Frame ( self, bg = "white", height = 100, width = 300 )
        #self.testFrame.grid()

if __name__ == "__main__":
    main()

`

Now here’s the problem: This piece of code does everything I want it to do. There are three frames here, the top frame self.f1, the middle frame self.f2, the bottom frame self.f3, and a simple quit button. Top (row 0) and bottom (row 2) frames resize horizontally (column 0) and the middle frame’s row (row 1, column 0) receives all of the weight for resizing horizontally and vertically. Everything is good… UNTIL I try to implement my own class myTestFrame.

Whenever I try to populate a custom frame in self.f2 with say a Text() widget, things go haywire. It displays the default text widget but my resizing goes funky, GRID placement is crazy. What is going on?

EDIT!!!
unutbu’s answer below certainly helps with the class issue. Now the GRID manager is placing a text widget correctly inside the middle frame self.f2, however, even when I set the sticky in myText.grid(sticky = N+S+E+W) it does not expand correctly. So I thought the following:

1) By setting the root window to be resizable with the def __init__ constructor of class App, I set it so that the first row and zeroth column of the App window would receive all of the space allotment for resizing in self.rowconfigure( 1, weight = 1) and self.columnconfigure( 0, weight = 1).

2) Now that the frame self.f2 is placed in row 1, it will receive all of the resizing with self.f2.grid(N+S+E+W).

3) Now I populate this custom frame with a text() widget that is set to occupy ALL of the space in this frame by stating self.myText.grid( sticky = N+S+E+W).

But the text() widget is not filling self.f2! When I do this normally without a custom class, using the sticky = N+S+E+W attribute works correctly. Am I thinking about this correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:06:53+00:00Added an answer on May 26, 2026 at 8:06 am

    The Frame.__init__ needs to be passed the parent:

    class myTestFrame( tk.Frame ):
        tk.Frame.__init__(self, parent, *args, **kwargs)
    

    Runnable example:

    import sys
    import Tkinter as tk
    E=tk.E
    W=tk.W
    N=tk.N
    S=tk.S
    def main():
        app = App()
        app.master.title("Sample application")
        app.mainloop()
    
    class App(tk.Frame):
        def __init__(self, master=None):
            tk.Frame.__init__(self, master)
            self.grid(sticky=N+S+E+W)
    
            top = self.winfo_toplevel()
            top.rowconfigure(0, weight=1)
            top.columnconfigure(0, weight=1)
    
            self.rowconfigure(1, weight=1)
            self.columnconfigure(0, weight=1)
            self.__createWidgets()
    
        def __createWidgets(self):
            self.f1 = tk.Frame(self, height=100, width=200, bg='green')
            self.f1.grid(row=0, sticky=E+W)
    
            self.f2 = myTestFrame(self, bg='yellow', height=100, width=200)
    
            self.f3 = tk.Frame( self, bg = "cyan", height = 100, width = 200)
            self.f3.grid(row=2, sticky=E+W)
    
            self.quitButton = tk.Button(self, text="Quit", command=self.quit)
            self.quitButton.grid(row=4, column=0, sticky=E+W)
    
    class myTestFrame( tk.Frame ):
        def __init__(self, parent, cnf={}, **kw):
            tk.Frame.__init__(self, parent, cnf, **kw)
            self.grid(row=1, sticky=N+S+E+W)
            self.rowconfigure(0, weight=1)
            self.columnconfigure(0, weight=1)                
            self.myText = tk.Text(self)
            self.myText.grid(row=0, sticky=N+S+E+W)
    
    if __name__ == "__main__":
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem when using group-by. Here is the code (defn combine-by-coords [values]
Im having this problem when changing my entities connection string. here is the code:
I'm having a problem with finding N positions in a grid based on a
I'm having a problem with the conversion to Excel code I'm finding. I am
I'm having a problem with FBDialog. Apparently after the either the email or the
I am having a problem with my query that includes a 'like' statement. Here
I am having a problem saving a Domain object. Below is my Controller code:
I'm having a problem with either typecasting or object scope. I'm getting an uncaught
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
We're having problem with a huge number of legacy stored procedures at work. Do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.