-2

I'm trying to grid a label inside its parent frame but it instead shows up at the bottom of the window, nowhere that I can think is even related to where I told it to go. Here is my code:

class Window:
def __init__(self, root):
    #configure the window as root
    self.root = root

    #create a canvas
    self.canvas = Canvas(self.root, bg="white", width=500, height=300).grid(row=0, column=0, rowspan=2)

    #create two frames to the right of the canvas for entering letters and displaying wrong guesses
    self.wrong_guess_frame = Frame(self.root, bg="red", width=150, height=150).grid(row=0, column=1)
    self.entry_frame = Frame(self.root, bg="green", width=150, height=150).grid(row=1, column=1)

    #create a title label for the wrong answers and a frame in which to sit it
    self.wrngguess_title_frame = Frame(self.wrong_guess_frame, bg="blue",width = 100).grid()
    self.wrngguess_title = Label(self.wrong_guess_frame, text="Wrong Guesses:").grid()

The last two lines are what I'm having trouble with. The self.wrngguess_title appears not in the wrong_guess_frame but at the bottom of the window, and I can't figure out what's causing that.

Any help would be appreciated. TIA

2 Answers2

1

self.wrong_guess_frame does NOT refer to any frame; this variable is in fact None, the return value of the .grid() method you applied to the frame after you created it. And None, as the first parameter to any widget constructor, gets interpreted as a reference to the root window.

You have to do the widget creation and grid/pack as two separate statements if you want to also have a variable refer to the widget:

self.wrong_guess_frame = Frame(self.root, bg="red", width=150, height=150)
self.wrong_guess_frame.grid(row=0, column=1)
jasonharper
  • 9,450
  • 2
  • 18
  • 42
0

It seems that, based on what you specified in the quesiton, all you need to do is specify the row and the column to be the same as the frame, like so:

    self.wrngguess_title = Label(self.wrong_guess_frame, text="Wrong Guesses:").grid(row=0, column=1)
    self.wrngguess_title_frame = Frame(self.wrong_guess_frame, bg="blue",width = 100).grid(row=0, column=1)

The preceeding code produces the following result: Result

But if you want to move the line down a bit, use the following snippet:

self.wrngguess_title_frame = Frame(self.wrong_guess_frame, bg="blue",width = 100).grid(row=0, column=1, pady=(25,10))

Which produces this result: Result

Hope that helps you out. That's what I deduced you wanted from your question.

lyxαl
  • 1,108
  • 1
  • 16
  • 26