Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Classes in Tkinter's Grid Manager
    primarykey
    data
    text
    <p>I am having a problem with either Tkinter's GRID geometry manager or custom classes or both. Most likely both. Here's the code:</p> <pre><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() </code></pre> <p>`</p> <p>Now here's the problem: This piece of code does everything I want it to do. There are three frames here, the top frame <code>self.f1</code>, the middle frame <code>self.f2</code>, the bottom frame <code>self.f3</code>, 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 <code>myTestFrame</code>. </p> <p>Whenever I try to populate a custom frame in <code>self.f2</code> with say a <code>Text()</code> widget, things go haywire. It displays the default text widget but my resizing goes funky, GRID placement is crazy. What is going on? </p> <p>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 <code>self.f2</code>, however, even when I set the sticky in <code>myText.grid(sticky = N+S+E+W)</code> it does not expand correctly. So I thought the following:</p> <p>1) By setting the root window to be resizable with the <code>def __init__</code> constructor of <code>class App</code>, 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 <code>self.rowconfigure( 1, weight = 1)</code> and <code>self.columnconfigure( 0, weight = 1)</code>. </p> <p>2) Now that the frame <code>self.f2</code> is placed in row 1, it will receive all of the resizing with <code>self.f2.grid(N+S+E+W)</code>. </p> <p>3) Now I populate this custom frame with a <code>text()</code> widget that is set to occupy ALL of the space in this frame by stating <code>self.myText.grid( sticky = N+S+E+W)</code>. </p> <p>But the <code>text()</code> widget is not filling <code>self.f2</code>! When I do this normally without a custom class, using the <code>sticky = N+S+E+W</code> attribute works correctly. Am I thinking about this correctly?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload