Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

What I have tried:

Python
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.
Posted
Updated 31-Jan-21 6:23am
Comments
Richard MacCutchan 29-Jan-21 3:55am    
I think it is because you have declared the window as having only three columns (columnspan = 3). So the equals button is moved outside of the allowed columns. It may be possible by adding the last three to a groupbox and then putting that in the last space, with a columnspan value of 2.

You will need to try a few things, but take a look at Python - Tkinter grid() Method - Tutorialspoint[^] for assistance.
Nick_is_asking 29-Jan-21 5:09am    
I can't figure it out.I tried a lot.Can anyone help me?
Nick_is_asking 29-Jan-21 14:41pm    
Anyone?

1 solution

I modified your code. I can propose this solution for your problem:
root = Tk()
root.geometry("403x440")
root.title("Calculator")
window = Entry(root, width = 40).grid(padx = 35, columnspan = 3)

button_7 = Button(root, text = "7", pady = 40).grid(row = 1, column = 0, columnspan = 2, sticky='nesw')
button_8 = Button(root, text = "8").grid(row = 1, column = 2, columnspan = 2, sticky='nesw')
button_9 = Button(root, text = "9").grid(row = 1, column = 4, columnspan = 2, sticky='nesw')

button_4 = Button(root, text = "4", pady = 40).grid(row = 2, column = 0, columnspan = 2, sticky='nesw')
button_5 = Button(root, text = "5").grid(row = 2, column = 2, columnspan = 2, sticky='nesw')
button_6 = Button(root, text = "6").grid(row = 2, column = 4, columnspan = 2, sticky='nesw')

button_1 = Button(root, text = "1", pady = 40).grid(row = 3, column = 0, columnspan = 2, sticky='nesw')
button_2 = Button(root, text = "2").grid(row = 3, column = 2, columnspan = 2, sticky='nesw')
button_3 = Button(root, text = "3").grid(row = 3, column = 4, columnspan = 2, sticky='nesw')

button_0 = Button(root, text = "0").grid(row = 4, column = 0, columnspan = 3, sticky='nesw')

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

root.mainloop()

I reduce your button to half of its original size and make it occuppy 2 colunm instead of one. The sticky make your button occupy all the space available in the specified colunm.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900