我正在尝试将图像面板放在一个表单上,这样当单击一个按钮时,在程序启动时放在面板上的64x64图像将被更大的320x224图像替换 - 像素大小并不那么重要因为它们的大小不同。我几乎已经得到了它 - 现在图像都加载了,当点击按钮时确实把第二个放了 - 不幸的是它是第二个图像的左上角64x64,而不是整个图像。
必须可以调整面板的大小,以便可以查看整个图像吗?这是我的代码:
#First we create our form elements. This app has a label on top of a button, both below a panel with an image on it, so we create a sizer and the elements
self.v_sizer = wx.BoxSizer(wx.VERTICAL)
self.imagePanel = wx.Panel(self, -1)
self.FileDescriptionText = wx.StaticText(self, label="No file loaded")
self.openFileDialog = wx.Button(self, label="Load a file", size=(320,40))
#Bind the button click to our press function
self.openFileDialog.Bind(wx.EVT_BUTTON, self.onOpenFileDialog)
#That done, we need to construct the form. First, put the panel, button and label in the vertical sizer...
self.v_sizer.Add(self.imagePanel, 0, flag=wx.ALIGN_CENTER)
self.v_sizer.Add(self.openFileDialog, 0, flag=wx.ALIGN_CENTER)
self.v_sizer.Add(self.ROMDescriptionText, 0, flag=wx.ALIGN_CENTER)
#then assign an image for the panel to have by default, and apply it
self.imageToLoad = wx.Image("imgs/none_loaded.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.imageCtrl = wx.StaticBitmap(self.imagePanel, -1, self.imageToLoad, (0, 0), (self.imageToLoad.GetWidth(), self.imageToLoad.GetHeight()))
#Set the sizer to be owned by the window
self.SetSizer(self.v_sizer)
#Set the current window size to the size of the sizer
self.v_sizer.Fit(self)
#Set the Minimum size of the window to the current size of the sizer
self.SetMinSize(self.v_sizer.GetMinSize())
def onOpenFileDialog(self, event):
img = wx.Image("imgs/title.png", wx.BITMAP_TYPE_ANY)
self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
self.imagePanel.Refresh()
(它被称为onOpenFileDialog,因为它最终将从组合框路径中拾取图像。)
如何编辑onOpenFileDialog方法,例如它首先找到图像大小,就像在初始表单元素创建中的self.imageCtrl行中一样?我找不到办法。
答案 0 :(得分:3)
尝试在self.v_sizer.Fit(self)
方法
onOpenFileDialog()