By anandu4115
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!
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:
Frame widget to contain the button. This allows us to use the pack method to center the button vertically within the window.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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.