简体   繁体   中英

Centering button using grid() in tkinter

I am simply trying to center some buttons in Python and it won't work even after looking at other a dozen threads.

This is my code at the moment:

import tkinter as tk

HEIGHT = 600
WIDTH = 1000

root = tk.Tk()

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

frame = tk.Frame(root, bg='#000A01')
frame.place(relwidth=1, relheight=1)

button0 = tk.Button(frame, text="LEFT", relief='flat', justify='center', bg='#000A01', fg='#00A010', padx='35', width='0', font=('Monofonto', 18))
button0.grid(row=0, column=0)
button1 = tk.Button(frame, text="CENTER", relief='flat', justify='center', bg='#000A01', fg='#00A010', padx='35', width='0', font=('Monofonto', 18))
button1.grid(row=0, column=1)
button2 = tk.Button(frame, text="RIGHT", relief='flat', justify='center', bg='#000A01', fg='#00A010', padx='35', width='0', font=('Monofonto', 18))
button2.grid(row=0, column=2)

root.mainloop()

this is the output

outputofabovecode

I want all buttons to be centered in the middle of the window and if I were to add more buttons in the same row it would add more to the center.

To center a button using place:

place(relx=0.5, rely=0.5, anchor=CENTER) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM