Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Created June 18, 2024 21:20
Show Gist options
  • Save vigilantPotato/07840f3a1ceb8b807e6fe559c1b8a4e8 to your computer and use it in GitHub Desktop.
Save vigilantPotato/07840f3a1ceb8b807e6fe559c1b8a4e8 to your computer and use it in GitHub Desktop.
centering tkinter main window
import ctypes
import tkinter
def centering_main_window(event):
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = root.winfo_width()
window_height = root.winfo_height()
x = screen_width / 2 - window_width /2
y = screen_height / 2 - window_height / 2
root.geometry("+%d+%d" % (x, y))
if __name__ == "__main__":
ctypes.windll.shcore.SetProcessDpiAwareness(1)
root = tkinter.Tk()
root.geometry("500x500")
root.bind("<Visibility>", centering_main_window)
label = tkinter.Label(root, text="center", font=("Helvetica", "17"))
label.pack(anchor="center", expand=1)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment