Beno Terzic
Beno Terzic

Reputation: 43

How do I place tkinter buttons in a specific area?

I am making a program for finding booking contacts with ease. When I type in all of this information, the buttons stay to the top left of the window.

How can I get them in the middle of the screen? Better, how would I place them where-ever I want in general? (Don't worry about the webbrowser import for that is for later in the program.)

import webbrowser
from tkinter import *
from tkinter import ttk

root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)

style = ttk.Style()
style.configure("TButton", 
                font="TkDefaultFont", 
                height=20, 
                width=20,
                padding=10)
main_frame = Frame()
main_frame.grid(row=0, columnspan=4)

# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid(row=1, column=3)
button_name = ttk.Button(main_frame, text='Name').grid(row=2, column=3)
button_email = ttk.Button(main_frame, text='Email').grid(row=3, column=3)




root.mainloop()

Upvotes: 0

Views: 233

Answers (3)

YJiqdAdwTifMxGR
YJiqdAdwTifMxGR

Reputation: 123

if you want them to me in the middle of the screen, use .pack instead of .grid.

import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)

style = ttk.Style()
style.configure("TButton", 
                font="TkDefaultFont", 
                height=20, 
                width=20,
                padding=10)
main_frame=Frame()

# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid()
button_name = ttk.Button(main_frame, text='Name').grid()
button_email = ttk.Button(main_frame, text='Email').grid()
main_frame.pack()
root.mainloop()

warning: do not use .pack and .grid in the same window.

Upvotes: 0

SF12 Study
SF12 Study

Reputation: 385

use the .place option instead of .grid:

...
Button = Button(something, text='Something', command=Something)
Button.place(x=value(e.g:2), y=value(e.g:2))
...

Upvotes: 1

cjonesrun
cjonesrun

Reputation: 146

You could use row/column configure to center up the buttons: http://www.effbot.org/tkinterbook/grid.htm I've added this under your root configuration. Ultimately, the position of widgets will depend on the widgets that you use and adjusting their position with the grid manager.

import webbrowser
from tkinter import *
from tkinter import ttk

root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)

root.grid_rowconfigure(0, weight=1)  # Added to center buttons
root.grid_columnconfigure(0, weight=1)  # Added to  center buttons

style = ttk.Style()
style.configure("TButton",
            font="TkDefaultFont",
            height=20,
            width=20,
            padding=10)
main_frame = Frame()


# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid(row=1, column=1)
button_name = ttk.Button(main_frame, text='Name').grid(row=2, column=1)
button_email = ttk.Button(main_frame, text='Email').grid(row=3, column=1)

main_frame.grid(row=0, column=0)


root.mainloop()

Upvotes: 0

Related Questions