调整 wxPython wx.Panel 的大小?
我正在尝试在表单上放置一个图像面板,这样当单击按钮时,程序启动时放在面板上的 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 行中一样在初始表单元素创建中?我找不到办法做到这一点。
I'm trying to place an image panel on a form such that when a button is clicked the 64x64 image that's put on the panel at program start is replaced with a bigger 320x224 image - the pixel sizes aren't so much important as they are being different sizes. I've ALMOST got it - right now the images both load and it does indeed put the second one up when the button's clicked - unfortunately it's the top left 64x64 of the second image, not the whole thing.
It must be possible to resize the panel so the whole image can be viewed, surely? Here's my code as is:
#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()
(It's called onOpenFileDialog as eventually it'll be picking the images from a combobox path.)
How can I edit the onOpenFileDialog method such as it finds the image size first, like it does in the self.imageCtrl line in the initial form element creation? I cannot find a way of doing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在
onOpenFileDialog()
方法末尾调用self.v_sizer.Fit(self)
Try calling
self.v_sizer.Fit(self)
at the end of youronOpenFileDialog()
method