Home > OS >  tkinter grid method not properly placing the button
tkinter grid method not properly placing the button

Time:07-22

I want to place a tkinter Button widget at specific coordinates within a window, but the button seems to ignore the coordinates and is always places at the top left corner of my window.

This is the code I have been running:

import tkinter as ter

root = ter.Tk()

root.minsize(500,500)

im = ter.PhotoImage(height=1, width=1)

my_button = ter.Button(root, height=1, width=1, image=im, bg="black")
my_button.grid(row=250, column=250)

root.mainloop()

This is the result:

screenshot of result

You can see that the button is placed at (0,0) even though I have mentioned (250,250) as its coordinates. I am not sure if I have a bug in my machine. How can I achieve desired behaviour of the button widget?

CodePudding user response:

If you are using the .grid(row=x,column=x) method, yet there are no other columns/rows, your image will be put in the first (zeroeth) row and in the first (zeroeth) column. Because of that, either fill the other columns with blanks by saying:

blank = Label(root,text = " ")
blank.grid(row=0,column=0)
blank.grid(row=1,column=1)

etc.

Or you could add padx,pady,idapx,ipady settings to your image.

Another solution would be trying to use the .place() method which is described screenshot

  • Related