I want to add an image to my interface, but the picture is simply too big and I don't know how to change the size or the position.
I can only show the picture, but the picture is too big and covers everything. So here is the code:
self.png = wx.StaticBitmap(self, -1, wx.Bitmap("image.png", wx.BITMAP_TYPE_ANY))
The quickest way would be to use a copy of your image, resized
as appropriate.
However, you can use wx.Image
which provides options Scale
or Rescale
along with Resize
, Rotate
etc. For example:
import wx
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.MaxImageSize = 500
self.Image = wx.StaticBitmap(self, bitmap=wx.Bitmap(self.MaxImageSize, self.MaxImageSize))
box = wx.BoxSizer(wx.VERTICAL)
box.Add(self.Image, 0, wx.ALL, 10)
self.SetSizer(box)
Img = wx.Image('image/pia21970-opt.jpg', wx.BITMAP_TYPE_ANY)
# scale the image, keeping ratio
W = Img.GetWidth()
H = Img.GetHeight()
print("Actual image size Width",W,"Height",H)
if W > H:
NewW = self.MaxImageSize
NewH = self.MaxImageSize * H / W
else:
NewH = self.MaxImageSize
NewW = self.MaxImageSize * W / H
print("New image size Width",NewW,"Height",NewH)
Img = Img.Scale(NewW,NewH,quality=wx.IMAGE_QUALITY_HIGH)
self.Image.SetBitmap(wx.Bitmap(Img))
self.Show()
class App(wx.App):
def OnInit(self):
frame = TestFrame(None, -1, "wxBitmap Test")
return True
if __name__ == "__main__":
app = App()
app.MainLoop()
Note: combining the ability to scale an image with a bit of judicious mouse scrolling can provide a Zoom control.
Positioning can be achieved using the form pos=(x,y)
if not using sizers
otherwise the sizer
controls the position and you control the sizer
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.