0

So i'm new using tkinter and im reading a book to learn, but i'm having trouble with this example using the pack() function. It is supposed to look like the buttons are centered but for me it doesn't work, it shows like if there was some other blank frame on the top of my actual frame.

Here's how it looks, and here is how is supposed to look.

Any help is apreciated. Thanks.

Here's the code:

from tkinter import *

class App:
    def __init__(self, fm):
        Button(fm, text='Left').pack(side=LEFT, expand = YES)
        Button(fm, text='Center').pack(side=LEFT, expand = YES)
        Button(fm, text='Right').pack(side=LEFT, expand = YES)


root = Tk()
fm = Frame(root, width=300, height=200).pack(expand=True, fill=BOTH)
root.option_add('*font', ('verdana', 12, 'bold'))
root.title("Pack - Example 1")
display = App(fm)
root.mainloop() 

Book: Python and tkinter programming by John E. Grayson

  • Problem is this line: `fm = Frame(root, width=300, height=200).pack(expand=True, fill=BOTH)`. Read [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name). – Henry Yik Apr 29 '20 at 03:48
  • I've tried doing this but it just prints like there is none width or height specifications. – Louie Fraga Apr 29 '20 at 04:03
  • The correct way to deal with this is using [`propagate`](https://stackoverflow.com/questions/563827/how-to-stop-tkinter-frame-from-shrinking-to-fit-its-contents). – Henry Yik Apr 29 '20 at 04:06
  • Thanks so much! It worked, im new in stack overflow so i don't know how to mark your comment as useful. But thanks! – Louie Fraga Apr 29 '20 at 04:11
  • @HenryYik:the use of `propagate` is almost never the right solution. It can sometimes fix one problem, but then causes others. – Bryan Oakley Apr 29 '20 at 04:32
  • @BryanOakley I understand, and that’s why I linked it to your answer which gave a very good explanation on it :) – Henry Yik Apr 29 '20 at 04:33

0 Answers0