Report this

What is the reason for this report?

tkinter python not changing the location of buttons

Posted on February 17, 2021

so i am trying to make a timer application wth gui using tkinter and python and i want the button to start the timer to be in the center of the window but when i change the row and column of the button, the location of the button in the window doesnt change and remains in the first row and column i cant fix it and i have googled it many times but cant find the answer this is my code:

from tkinter import * root = Tk()

root.title(‘Timer’) root.geometry(‘500x500’) button_2 = Button( root, text=2, padx=40, pady=20) button_2.grid(row=3, column=3)

root.mainloop()

pls help me fix this :D



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
from tkinter import *
root = Tk()

root.title('Timer')
root.geometry('500x500')
button2 = Button( root, text=2, padx=40, pady=20)
button2.pack()
button2.place(bordermode=INSIDE, relx=0.4, rely=0.4)

root.mainloop()

You could experiment with the relative height and relative width, but it will change if you resize the window manually through mouse drag.

If you want the window to not get resized, then you can follow below code.

from tkinter import *
root = Tk()

root.title('Timer')
root.geometry('500x500')
root.resizable(width=False, height=False)
button2 = Button( root, text=2, padx=40, pady=20)
button2.pack()
button2.place(bordermode=INSIDE, relx=0.4, rely=0.4)

root.mainloop()

It looks like there’s a small syntax error in the code, and the button is not appearing at the center of the window because the row and column values for the grid method are not set correctly. Let’s correct the code:

from tkinter import *

root = Tk()
root.title('Timer')
root.geometry('500x500')

# Create a frame to contain the button
frame = Frame(root)
frame.pack(expand=True)

# Create the button and add it to the frame
button_2 = Button(frame, text="Start Timer", padx=40, pady=20)
button_2.pack(pady=50)  # Add padding to push the button down

root.mainloop()

In this revised code:

  • We create a Frame widget to contain the button. This allows us to use the pack method to center the button vertically within the window.
  • The button is added to the frame using the pack method with appropriate padding (pady) to center it vertically.

Now, when you run this code, the “Start Timer” button should appear in the center of the window both horizontally and vertically. Adjust the padding value as needed to achieve the desired appearance.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.