Python Forum

Full Version: tkinter python button position problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.I 'am beginner with tkinter and I build a simple calculator app and I have a problem with position of 3 buttons (+ , - , =).
I have 2 photos to see my problem & understand.

1st photo

2nd photo

2nd photo is the original size of window.
So, I want to put (- , + and =) under button 2 and 3.
How can I do it?

Code:
from tkinter import *

root = Tk()
root.geometry("403x440")
root.title("Calculator")
window = Entry(root , width = 40).grid(row = 0 , column = 0, padx = 35 , columnspan = 3)   

button_7 = Button(root , text = "7"  , padx = 60, pady = 40).grid(row = 1 , column = 0)
button_8 = Button(root , text = "8"  , padx = 60, pady = 40).grid(row = 1 , column = 1)
button_9 = Button(root , text = "9"  , padx = 60, pady = 40).grid(row = 1 , column = 2)

button_4 = Button(root , text = "4"  , padx = 60, pady = 40).grid(row = 2 , column = 0)
button_5 = Button(root , text = "5"  , padx = 60, pady = 40).grid(row = 2 , column = 1)
button_6 = Button(root , text = "6"  , padx = 60, pady = 40).grid(row = 2 , column = 2)

button_1 = Button(root , text = "1"  , padx = 60, pady = 40).grid(row = 3 , column = 0)
button_2 = Button(root , text = "2"  , padx = 60, pady = 40).grid(row = 3 , column = 1)
button_3 = Button(root , text = "3"  , padx = 60, pady = 40).grid(row = 3 , column = 2)

button_0 = Button(root , text = "0"  , padx = 60, pady = 40).grid(row = 4 , column = 0)

button_minus = Button(root , text = "-"  , padx = 40, pady = 40).grid(row = 4 , column = 1)
button_add = Button(root , text = "+"  , padx = 40, pady = 40).grid(row = 4 , column = 2)
button_equal = Button(root , text = "="  , padx = 40, pady = 40).grid(row = 4 , column = 3)

root.mainloop()
Thanks in advance.
you can play with this a bit:
from tkinter import *
 
root = Tk()
root.geometry("403x440")
root.title("Calculator")
window = Entry(root, width = 40).grid(row = 0, column = 0, padx = 35, columnspan = 3)   
 
button_7 = Button(root, text = "7" , padx = 60, pady = 40).grid(row = 1, column = 0)
button_8 = Button(root, text = "8" , padx = 60, pady = 40).grid(row = 1, column = 1)
button_9 = Button(root, text = "9" , padx = 60, pady = 40).grid(row = 1, column = 2)
 
button_4 = Button(root, text = "4" , padx = 60, pady = 40).grid(row = 2, column = 0)
button_5 = Button(root, text = "5" , padx = 60, pady = 40).grid(row = 2, column = 1)
button_6 = Button(root, text = "6" , padx = 60, pady = 40).grid(row = 2, column = 2)
 
button_1 = Button(root, text = "1" , padx = 60, pady = 40).grid(row = 3, column = 0)
button_2 = Button(root, text = "2" , padx = 60, pady = 40).grid(row = 3, column = 1)
button_3 = Button(root, text = "3" , padx = 60, pady = 40).grid(row = 3, column = 2)
 
opframe  = Frame(root).grid(row=4, column=0)
button_0 = Button(opframe, text = "0" , padx = 60, pady = 40).grid(row = 4, column = 0)

button_minus = Button(opframe, text = "-" , padx = 40, pady = 40).grid(row = 4, column=1)
button_add =   Button(opframe, text = "+" , padx = 40, pady = 40).grid(row = 4, column=2)
button_equal = Button(opframe, text = "=" , padx = 40, pady = 40).grid(row = 4, column=3)

root.mainloop()
You might do better using place rather than grid for this.

from tkinter import *
  
root = Tk()
root.geometry("345x380")
root.title("Calculator")
root.resizable (False, False)
window = Entry(root, width = 40).place (x = 8, y = 10)   
  
button_7 = Button(root, text = "7" , padx = 50, pady = 30).place (x = 0, y = 40)
button_8 = Button(root, text = "8" , padx = 50, pady = 30).place (x = 115, y = 40)
button_9 = Button(root, text = "9" , padx = 50, pady = 30).place (x = 230, y = 40)

button_4 = Button(root, text = "4" , padx = 50, pady = 30).place (x = 0, y = 125)
button_5 = Button(root, text = "5" , padx = 50, pady = 30).place (x = 115, y = 125)
button_6 = Button(root, text = "6" , padx = 50, pady = 30).place (x = 230, y = 125)

button_1 = Button(root, text = "1" , padx = 50, pady = 30).place (x = 0, y = 210)
button_2 = Button(root, text = "2" , padx = 50, pady = 30).place (x = 115, y = 210)
button_3 = Button(root, text = "3" , padx = 50, pady = 30).place (x = 230, y = 210)

button_0 = Button(root, text = "0" , padx = 35, pady = 30).place (x = 0, y = 295)
button_minus = Button(root, text = "-" , padx = 35, pady = 30).place (x = 85, y = 295)
button_add =   Button(root, text = "+" , padx = 35, pady = 30).place (x = 166, y = 295)
button_equal = Button(root, text = "=" , padx = 35, pady = 30).place (x = 257, y = 295)
 
root.mainloop()
Grid layout works, but it takes a bit of work.
import tkinter as tk

def button(parent, text, row, column):
    """Make a button"""
    button = tk.Button(parent, text=text)
    button.grid(row=row, column=column, padx=1, pady=1, sticky='news')
    return button
    
root = tk.Tk()
root.title("Calculator")
root.geometry('250x250')

window = tk.Entry(root, width=40)
window.grid(row=0, column=0, columnspan=3, sticky='ew')   

number_buttons = [
    button(root, '0', 4, 0),
    button(root, '1', 3, 0),
    button(root, '2', 3, 1),
    button(root, '3', 3, 2),
    button(root, '4', 2, 0),
    button(root, '5', 2, 1),
    button(root, '6', 2, 2),
    button(root, '7', 1, 0),
    button(root, '8', 1, 1),
    button(root, '9', 1, 2)]

frame = tk.Entry(root, bd=0)
frame.grid(row=4, column=1, columnspan=2, sticky='news')   

op_buttons = {
    '-': button(frame, '-', 0, 0),
    '+': button(frame, '+', 0, 1),
    '=': button(frame, '=', 0, 2)}

for i in range(3):
    root.columnconfigure(i, weight=1)
    root.rowconfigure(i+1, weight=1)
    frame.columnconfigure(i, weight=1)
root.rowconfigure(4, weight=1)
frame.rowconfigure(0, weight=1)
    
root.mainloop()
There are 2 problems to overcome. The first is getting the grid to expand to fill the window. The second is to get the buttons to expand to fill their grid space.

This code tells the root and frame grids how they should expand to fill available space. By default the "weight" defaults to 0, and none of the columns or rows resize to fit their parent. Without the configure code the buttons use their default sizing which may be too large to fit the window or too small to fill the window.
for i in range(3):
    root.columnconfigure(i, weight=1)
    root.rowconfigure(i+1, weight=1)
    frame.columnconfigure(i, weight=1)
root.rowconfigure(4, weight=1)
frame.rowconfigure(0, weight=1)
Configuring the grid rows and columns will expand the grid to fit the window, but it doesn't make the buttons grow and shrink with the window. To get that you need to set the sticky. This command tells frame that it "stick" to the North, East, West and South edges of the grid space.
frame.grid(row=4, column=1, columnspan=2, sticky='news')