6
$\begingroup$

I'm in-setting a grid of buttons over an image, and I think it's probably a very simple thing that I'm missing. In the example below Scaled doesn't work to stretch the Button's properly:

Graphics[{Inset[ExampleData[{"TestImage", "Lena"}], Scaled[{0, 1}], 
   Scaled[{0, 1}], Scaled[1]],
  Inset[Button["test", Print[1], Background -> RGBColor[1, 0, 0, 0.3],
     ImageSize -> Automatic], Scaled[{0, 1}], Scaled[{0, 1}], 
   Scaled[{1, 1/2}]],
  Inset[Button["test", Print[1], Background -> RGBColor[0, 1, 0.3], 
    ImageSize -> Automatic], Scaled[{0, 1/2}], Scaled[{0, 1/2}], 
   Scaled[{1, 1/2}]]
  }, PlotRange -> {{0, 1}, {0, 1}}]

enter image description here

It should look like this:

enter image description here

The above I made with this:

Overlay[{ExampleData[{"TestImage", "Lena"}], 
  TextGrid[{{Button["test", Print[1], 
      Background -> RGBColor[1, 0, 0, 0.3], 
      ImageSize -> {512, 256}]}, {Button["test", Print[2], 
      Background -> RGBColor[0, 1, 0, 0.3], 
      ImageSize -> {512, 256}]}}, Spacings -> {0, 0}]}]

Which would be a solution except that:

  • I don't want to give explicit ImageSizes
  • Overlay kills the buttons
$\endgroup$
5
$\begingroup$

You can use the second and third arguments of Overlay:

image = ExampleData[{"TestImage", "Lena"}]; 
Overlay[{image,  
  TextGrid[{{Button["test", Print[1], Background -> RGBColor[1, 0, 0, 0.3], 
      ImageSize -> ImageDimensions[image]/{1, 2}]},
    {Button["test",  Print[2], Background -> RGBColor[0, 1, 0, 0.3], 
      ImageSize -> ImageDimensions[image]/{1, 2}]}}, 
   Spacings -> {0, 0}]}, All, 2]

Alternatively, use ImageSize -> {Scaled[1], Scaled[1/2]} with Button and use Scaled[1] as the size argument in Inset:

Graphics[{Inset[ExampleData[{"TestImage", "Lena"}], Scaled[{0, 1}], 
   Scaled[{0, 1}], Scaled[1]], 
 Inset[Button["test", Print[1], Background -> RGBColor[1, 0, 0, 0.3], 
    ImageSize -> {Scaled[1], Scaled[1/2]}], 
  Scaled[{0, 1}], Scaled[{0, 1}], Scaled[1]], 
 Inset[Button["test", Print[2], Background -> RGBColor[0, 1, 0, 0.3],
     ImageSize -> {Scaled[1], Scaled[1/2]}], 
  Scaled[{0, 1/2}], Scaled[{0, 1}], Scaled[1]]},
PlotRange -> {{0, 1}, {0, 1}}]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.