调整 wxPython wx.Panel 的大小?

发布于 2024-09-14 12:19:27 字数 1894 浏览 10 评论 0原文

我正在尝试在表单上放置一个图像面板,这样当单击按钮时,程序启动时放在面板上的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-09-21 12:19:27

尝试在 onOpenFileDialog() 方法末尾调用 self.v_sizer.Fit(self)

Try calling self.v_sizer.Fit(self) at the end of your onOpenFileDialog() method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文