1

I am trying to build a very simple GUI, done that before this way, never a problem. Right now, on a Mac, I can't seem to make it work.

This is the code

from Tkinter import *

class App:
    def __init__(self, master):

        # Gui Creation Block
        frame = Frame(master, bd=2, relief=SUNKEN)
        self.master = master

        self.top = Frame(frame,   height=150, width=700, relief=SUNKEN)
        self.left = Frame(frame , height=400, width=200, relief=SUNKEN)
        self.right = Frame(frame, height=400, width=500, relief=SUNKEN)

        self.top.grid(row=0, column=0, columnspan=2)

        self.left.grid(row=1, column=0)
        self.right.grid(row=1, column=1)

        # Sample Label
        self.lbl = Label(frame, text="Log file locaction" )
        self.lbl.grid(row=0, column=0)

        self.top.pack()

        frame.pack()

# Run the programs
root = Tk()
app = App(root)

root.mainloop()

As soon as I run that, the CPU usage skyrockets to 100% and stays there until I hard kill the python instance running there. If I comment out the label bit, that doesn't happen. So what is wrong here?

------ Update -------

Okay, changed that file locator thing yeah, that was a messy one. And removing the pack() from top solved the 100% cpu issue.

However, it still is not behaving like I want.

    self.top = Frame(frame,  bg="black", height=150, width=700, relief=SUNKEN)
    self.left = Frame(frame , height=400, width=200, relief=SUNKEN)
    self.right = Frame(frame, height=400, width=500, relief=SUNKEN)

Should create 3 blocks in which I can place other widgets, but as soon as I add the third label, like this;

    # Sample Label
    self.lbl = Label(self.top, text="Top" )
    self.lbl.pack()

    # Sample Label
    self.lbl2 = Label(self.left, text="Left" )
    self.lbl2.pack()

    # Sample Label
    self.lbl3 = Label(self.right, text="Right" )
    self.lbl3.pack()

It resizes my whole app down to minimum size. Am I wrong in thinking that internal widgets don't change their parent containers or am I making another mistake?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Eloque
  • 311
  • 2
  • 10
  • 1
    pack, grid together ? Use only grid method ! – dsgdfg Sep 17 '15 at 11:30
  • You use both pack and grid on `self.top`. – MrAlexBailey Sep 17 '15 at 11:36
  • A Tkinter Frame automatically resizes itself to fit it's contents, but there's a way to override that behaviour. I'll post some code shortly. – PM 2Ring Sep 17 '15 at 12:19
  • BTW, it's not a great idea to substantially change your question after it's been answered: you should just ask a new question. But I'll let you get away with it this time because your 1st question was pretty trivial. :) – PM 2Ring Sep 17 '15 at 12:29

1 Answers1

4

What dsgdfg & Jkdc said: you've got both self.top.grid() and self.top.pack().
Don't do that! As you've discovered, Tkinter doesn't behave well when you mix pack() & grid() in the one parent.

As for your Update...

A Tkinter Frame automatically resizes itself to fit its contents. Most of the time that's desirable, but fortunately there's a way to override that behaviour when you want to use a fixed Frame size.

Here's a slightly modified version of your code that uses different coloured backgrounds for the Frames to make it easier to see which one's where. (Yes, those colours look terrible, but you'd change them to something nicer once you've finished laying stuff out in your GUI). And I've added a borderwidth arg to the inner Frames so that the SUNKEN relief is visible. I've also adjusted the inner Frames' positioning a little, since you had the first Label in the same grid position in the outer Frame as the self.top Frame. Also, you were trying to use self.lbl for two different Labels.

from Tkinter import *

class App:
    def __init__(self, master):
        self.master = master

        # Gui Creation Block
        master.title("LumenScriptor")
        frame = Frame(master, bd=2, relief=SUNKEN)
        frame.pack()

        self.top = Frame(frame, bg="red", height=150, width=700, relief=SUNKEN, borderwidth=2)
        self.left = Frame(frame, bg="yellow", height=400, width=200, relief=SUNKEN, borderwidth=2)
        self.right = Frame(frame, bg="blue", height=400, width=500, relief=SUNKEN, borderwidth=2)

        self.top.grid(row=0, column=1, columnspan=2)
        self.left.grid(row=1, column=1)
        self.right.grid(row=1, column=2)

        self.top.pack_propagate(0)
        self.left.pack_propagate(0)
        self.right.pack_propagate(0)

        # Sample Label
        self.lbl = Label(frame, text="Log file location")
        self.lbl.grid(row=0, column=0)

        # Sample Label
        self.lbl1 = Label(self.top, text="Top" )
        self.lbl1.pack()

        #Sample Label
        self.lbl2 = Label(self.left, text="Left" )
        self.lbl2.pack()

        # Sample Label
        self.lbl3 = Label(self.right, text="Right" )
        self.lbl3.pack()

# Run the programs
root = Tk()
app = App(root)

root.mainloop()
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Okay, changed that file locator thing yeah, that was a messy one. And removing the pack() from top solved the 100% cpu issue. – Eloque Sep 17 '15 at 11:48
  • Excellent and insightfull. Thanks. Seems like I am being sloppy, tired probably. – Eloque Sep 17 '15 at 12:39
  • @Eloque: Don't worry about it. It's easy to mess things up like column & row numbers. So you just need to keep testing your code as you add stuff so that you can fix mistakes as soon as they happen, rather than after you've added another 50 widgets. :) PS. You now have enough rep to up-vote :D – PM 2Ring Sep 17 '15 at 12:44