¿Cómo especificar dónde se abre una ventana Tkinter?


¿Cómo puedo indicar a una ventana de Tkinter dónde abrir, en función de las dimensiones de la pantalla? Me gustaría que se abriera en el medio.

Author: xxmbabanexx, 2013-02-16

3 answers

Esta respuesta se basa en La respuesta de Rachel. Su código no funcionó originalmente, pero con algunos ajustes pude corregir los errores.

import tkinter as tk


root = tk.Tk() # create a Tk root window

w = 800 # width for the Tk root
h = 650 # height for the Tk root

# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen

# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)

# set the dimensions of the screen 
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

root.mainloop() # starts the mainloop
 55
Author: xxmbabanexx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:10:30

Prueba esto

import tkinter as tk


def center_window(width=300, height=200):
    # get screen width and height
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    # calculate position x and y coordinates
    x = (screen_width/2) - (width/2)
    y = (screen_height/2) - (height/2)
    root.geometry('%dx%d+%d+%d' % (width, height, x, y))


root = tk.Tk()
center_window(500, 400)
root.mainloop()

Fuente

 23
Author: Rachel Gallen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-27 10:29:35
root.geometry('250x150+0+0')

Use esto Los dos primeros parámetros son el ancho y el alto de la ventana. Los dos últimos parámetros son coordenadas de pantalla x e y. Puede especificar las coordenadas x e y necesarias

 9
Author: LordDraagon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-26 08:49:30