python - 如何向 wx.ScrolledPanel 添加项目?

标签 python scroll wxpython

我想在层次结构中添加动态捕获的图像(一个接一个)。我想将它们添加到 wx.ScrolledPanel

ScrolledPanel 定义 - 已更新

    self.hbox = wx.BoxSizer(wx.HORIZONTAL)
    #self.sizer.Add(self.hbox)
    self.scroll = scrolled.ScrolledPanel(self, id = -1, pos = wx.DefaultPosition, size = (500, 400), style= wx.SUNKEN_BORDER , name = "Scroll")
    self.scroll.SetupScrolling(10,10,10,10)
    #self.scroll.SetSizer(self.hbox)
    self.sizer.Add(self.scroll)    


   #add to scroll
   images = wx.StaticBitmap(self, id=-1, pos=wx.DefaultPosition,
                            size=(200,150),
                            style= wx.SUNKEN_BORDER)
   images.SetBitmap(bmp)
   self.hbox.Add(images, 1, wx.BOTTOM | wx.EXPAND | wx.ALL, 3)
   self.scroll.SetSizer(self.hbox)
   self.scroll.SetAutoLayout(1)
   self.scroll.SetupScrolling()
   self.SetSizerAndFit(self.sizer)
   self.Refresh() 
   self.Layout() 
  • Python 2.6,Windows 32 位

更新后 - 我看到 scrollpanel 并将图像添加到 sizer。但是 sizer 没有显示在 scrollPanel 中。

最佳答案

这是您想要的一个粗略但可运行的示例,其中有一个小故障,我认为我还没有找出原因! (你只需要将一个名为“image.jpg”的缩略图大小的jpg放在与脚本相同的目录中即可)

import wx
import  wx.lib.scrolledpanel as scrolled

class ImageDlg(wx.Dialog):
    def __init__(self, parent, title):
        wx.Dialog.__init__(self, parent=parent,title=title, size=wx.DefaultSize)

        self.scrollPnl = scrolled.ScrolledPanel(self, -1, size=(200, 200), style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER)

        self.addBtn = wx.Button(self, id=wx.ID_ADD)
        self.Bind(wx.EVT_BUTTON, self.on_add, self.addBtn)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)       

        self.scrollPnlSizer = wx.BoxSizer(wx.VERTICAL)       
        img = wx.Image("image.jpg", wx.BITMAP_TYPE_ANY)
        staticBitmap = wx.StaticBitmap(self.scrollPnl, wx.ID_ANY, wx.BitmapFromImage(img))
        self.scrollPnlSizer.Add(staticBitmap, 1, wx.EXPAND | wx.ALL, 3)

        self.mainSizer.Add(self.addBtn)
        self.mainSizer.Add(self.scrollPnl)

        self.SetSizerAndFit(self.mainSizer)


    def on_add(self, event):
        img = wx.Image("image.jpg", wx.BITMAP_TYPE_ANY)
        staticBitmap = wx.StaticBitmap(self.scrollPnl, wx.ID_ANY, wx.BitmapFromImage(img))
        self.scrollPnlSizer.Add(staticBitmap, 1, wx.EXPAND | wx.ALL, 3)
        self.scrollPnl.SetSizer(self.scrollPnlSizer)
        self.scrollPnl.SetAutoLayout(1)
        self.scrollPnl.SetupScrolling()  

        self.Refresh()
        self.Layout()

class TestPanel(wx.Panel):     
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)

        openDlg_btn = wx.Button(self, label="Open Dialog")
        self.Bind(wx.EVT_BUTTON, self.onBtn)

        mainSizer = wx.BoxSizer(wx.HORIZONTAL)
        mainSizer.Add(openDlg_btn, 0, wx.ALL, 10)
        self.SetSizerAndFit(mainSizer)
        self.Centre()

    def onBtn(self, event):
        dlg = ImageDlg(self, title='Image Dialog')
        dlg.SetSize((300,300))

        dlg.CenterOnScreen()
        dlg.ShowModal()  
        dlg.Destroy()


class TestFrame(wx.Frame):    
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        TestPanel(self)


if __name__ == "__main__":

    app = wx.PySimpleApp()
    frame = TestFrame(None)
    frame.Show()
    app.MainLoop()

关于python - 如何向 wx.ScrolledPanel 添加项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3582696/

相关文章:

python - 构建 OpenSSL 以编译 Python 的可移植版本

wxpython - 为什么不推荐在 wxPython 中使用 ID?

python - 将 AuiManager 放入 AuiNotebook 页面中

python - 通过多索引列切片谓词过滤 DataFrame 中的行

python - 均值的 Numpy 语法

python 3.+,根据数据透视表中的平均价格创建一个新的分类列

javascript - 如何更改长滚动页面上的 URL?

java - ScrollMode.FORWARD_ONLY

html - 防止图像换行但使用滚动

android - JetCreator 是否仍在维护(和/或 JETPlayer 是否已弃用?)