Home > Software engineering >  How to center a Canvas Widget on Tkinter
How to center a Canvas Widget on Tkinter

Time:05-29

I'm learning tkinter for a project, but I'm having trouble knowing why is my layout not the way I want it to be. The terminal shows no errors so I don't think I have some argument missing or something like that. This is how it currently looks and I intend for it to be like this.

Thanks!

import tkinter as tk
import tkinter.ttk

root = tk.Tk()
root.geometry('1920x1080')
root.configure(bg='blue')
seleccion = tk.IntVar()
seleccion.set(1)

canvas_input = tk.Canvas(root, width='1280', height='720')
canvas_input.configure(bg='white')
canvas_input.place(anchor='center')

tk.Label(root, text="Análisis meterológico").grid(row=0, column=1)
tk.Label(root, text="Bienvenido a nuestro proyecto!").grid(row=1, column=1)

entry1 = tk.Entry(root)

def obtenerCiudad():
    ciudad = entry1.get()
    label1 = tk.Label(root, text= ciudad)
    canvas_input.create_window(400, 200, window=label1.grid(row=4, column=2))

def ShowChoice():
    opcion = seleccion.get()
    label2 = tk.Label(root, text= opcion)
    canvas_input.create_window(400, 200, window=label2.grid(row=19, column=0))

opciones = [("Coordenadas de la ubicación", 1),
        ("Velocidad del viento", 2),
        ("Humedad actual", 3),
        ("Temperatura actual", 4),
        ("Estado del clima", 5),
        ("Lluvia actual", 6),
        ("Nubosidad actual", 7),
        ("Luz ultra violeta", 8),
        ("Calidad del aire", 9),
        ("Estadisticas completas", 10),
        ("Salir del programa", 11)]

canvas_input.create_window(400, 200, window=tk.Label(root, text="Qué desea consultar?").grid(row=2, column=0))
rows = 3

for opcion, val in opciones:
    rows  = 1
    boton = tk.Radiobutton(root, text=opcion, variable=seleccion, command=ShowChoice, value=val).grid(row=rows, column=0)
    canvas_input.create_window(400, 200, window=boton)
    
canvas_input.create_window(400, 200, window=tkinter.ttk.Separator(root, orient='vertical').grid(column=1, row=2, rowspan='19', sticky='ns'))
canvas_input.create_window(400, 200, window=tk.Label(root, text="Ingrese la ciudad a la que desea consultar: ").grid(row=2, column=2))
canvas_input.create_window(400, 200, window=entry1.grid(row=2, column=3))
canvas_input.create_window(400, 200, window=tk.Button(text='Consultar', command=obtenerCiudad).grid(row=20, column=1))

root.mainloop()

CodePudding user response:

If you want to use the center option you need to have the relx and rely to go with it and then call tk.CENTER for the anchor. Then you have to change your widgets to go inside your canvas_input and not root.

import tkinter as tk
import tkinter.ttk

root = tk.Tk()
root.geometry('1920x1080')
root.configure(bg='blue')
seleccion = tk.IntVar()
seleccion.set(1)

canvas_input = tk.Canvas(root, width='1280', height='720')
canvas_input.configure(bg='white')
canvas_input.place(relx=.5, rely=.5, anchor=tk.CENTER)

tk.Label(canvas_input, text="Análisis meterológico").grid(row=0, column=1)
tk.Label(canvas_input, text="Bienvenido a nuestro proyecto!").grid(row=1, column=1)

entry1 = tk.Entry(canvas_input)

def obtenerCiudad():
    ciudad = entry1.get()
    label1 = tk.Label(canvas_input, text= ciudad)
    canvas_input.create_window(400, 200, window=label1.grid(row=4, column=2))

def ShowChoice():
    opcion = seleccion.get()
    label2 = tk.Label(canvas_input, text= opcion)
    canvas_input.create_window(400, 200, window=label2.grid(row=19, column=0))

opciones = [("Coordenadas de la ubicación", 1),
        ("Velocidad del viento", 2),
        ("Humedad actual", 3),
        ("Temperatura actual", 4),
        ("Estado del clima", 5),
        ("Lluvia actual", 6),
        ("Nubosidad actual", 7),
        ("Luz ultra violeta", 8),
        ("Calidad del aire", 9),
        ("Estadisticas completas", 10),
        ("Salir del programa", 11)]

canvas_input.create_window(400, 200, window=tk.Label(canvas_input, text="Qué desea consultar?").grid(row=2, column=0))
rows = 3

for opcion, val in opciones:
    rows  = 1
    boton = tk.Radiobutton(canvas_input, text=opcion, variable=seleccion, command=ShowChoice, value=val).grid(row=rows, column=0)
    canvas_input.create_window(400, 200, window=boton)
    
canvas_input.create_window(400, 200, window=tkinter.ttk.Separator(canvas_input, orient='vertical').grid(column=1, row=2, rowspan='19', sticky='ns'))
canvas_input.create_window(400, 200, window=tk.Label(canvas_input, text="Ingrese la ciudad a la que desea consultar: ").grid(row=2, column=2))
canvas_input.create_window(400, 200, window=entry1.grid(row=2, column=3))
canvas_input.create_window(400, 200, window=tk.Button(canvas_input, text='Consultar', command=obtenerCiudad).grid(row=20, column=1))

root.mainloop()
  • Related