How do I place my buttons inside the gray frame?

29 views Asked by At

I'm trying to put my 3 buttons inside the gray frame (labeled mainframe), even though I put the parent of the buttons as the frame.

import tkinter

class theUI():
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.geometry("500x600")
        self.mainframe = tkinter.Frame(self.root, height=300, width=400, bg="gray").pack(side="left")
        tkinter.Button(master=self.mainframe, text="Rock").pack(side="left")
        tkinter.Button(master=self.mainframe, text="Paper").pack()
        tkinter.Button(master=self.mainframe, text="Scissors").pack(side="right")

        self.root.mainloop()


game = theUI()

I tried using pack and grid but they both gave me the same issue. I just started using tkinter so I'm still learning how the widgets work.

1

There are 1 answers

0
acw1668 On BEST ANSWER

Note that self.mainframe is None because it is the result of .pack(...). So the buttons will be children of root window because their parent is None.

You need to separate the following line:

self.mainframe = tkinter.Frame(self.root, height=300, width=400, bg="gray").pack(side="left")

into two lines:

self.mainframe = tkinter.Frame(self.root, height=300, width=400, bg="gray")
self.mainframe.pack(side="left")