# Center a GLFW Window at Screen using Python

**URL:** <https://discourse.glfw.org/t/center-a-glfw-window-at-screen-using-python/1618>\
**Category:** share\
**Created:** [July 22, 2020, 5:47pm UTC](https://discourse.glfw.org/t/center-a-glfw-window-at-screen-using-python/1618 "2020-07-22T17:47:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![8Observer8](https://avatars.discourse-cdn.com/v4/letter/8/bbe5ce/32.png) [@8Observer8](https://discourse.glfw.org/u/8Observer8)\
**Post date:** [July 22, 2020, 5:47pm UTC](https://discourse.glfw.org/t/center-a-glfw-window-at-screen-using-python/1618/1 "2020-07-22T17:47:34Z")

</div>

Hi,

I share the example for beginners how to center a window at screen because I did not find it by myself in the Internet and I wrote it from scratch. I hope it will useful for someone.

```python
import glfw

if not glfw.init():
    raise Exception("Failed to initialize the GLFW library")

window = glfw.create_window(400, 300, "My Centered Window", None, None)
monitor = glfw.get_primary_monitor()
pos = glfw.get_monitor_pos(monitor)
size = glfw.get_window_size(window)
mode = glfw.get_video_mode(monitor)
print(int((mode.size.height - size[1]) / 2))
glfw.set_window_pos(
    window,
    int(pos[0] + (mode.size.width - size[0]) / 2),
    int(pos[1] + (mode.size.height - size[1]) / 2))

if not window:
    glfw.terminate()
    raise Exception("Failed to create the GLFW window")

glfw.make_context_current(window)

while not glfw.window_should_close(window):
    glfw.poll_events()
    glfw.swap_buffers(window)

glfw.terminate()

```

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 22, 2020, 7:06pm UTC](https://discourse.glfw.org/t/center-a-glfw-window-at-screen-using-python/1618/2 "2020-07-22T19:06:29Z")

</div>

Many thanks for the code example!
