Python Forum
tkinter python button position problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter python button position problem
#1
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.
Reply
#2
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()
Reply
#3
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()
Reply
#4
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a function interrupt button tkinter AnotherSam 2 214 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 323 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  problem with radio button crook79 3 539 Aug-12-2021, 02:30 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 1,061 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter text widget word wrap position chrisdb 6 2,265 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  tkinter button image Nick_tkinter 4 1,328 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  Python3 tkinter radiobutton problem Nick_tkinter 14 1,543 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  TKinter restarting the mainloop when button pressed zazas321 7 3,491 Jan-26-2021, 06:38 AM
Last Post: zazas321
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 1,155 Dec-28-2020, 10:00 PM
Last Post: nanok66
  [Tkinter] Button click problem using OOP JohnB 5 1,034 Oct-21-2020, 12:43 PM
Last Post: JohnB

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020