Creating ScrolledWindow in wxPython

浪尽此生 提交于 2019-12-11 06:33:30

问题


I am trying to make a ScrolledWindow that can scroll over a grid of images, but the scrollbar isn't appearing. wxWidgets documentation says:

The most automatic and newest way [to set the scrollbars in wxScrolledWindow] is to simply let sizers determine the scrolling area. This is now the default when you set an interior sizer into a wxScrolledWindow with wxWindow::SetSizer. The scrolling area will be set to the size requested by the sizer and the scrollbars will be assigned for each orientation according to the need for them and the scrolling increment set by wxScrolledWindow::SetScrollRate

So I try to set the sizer of my ScrolledWindow with a GridSizer but it's not working. The code:

import wx

class MyFrame(wx.Frame):

    def __init__(self, parent, id=-1,title="",pos=wx.DefaultPosition,
         size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE,
         name="frame"):

        wx.Frame.__init__(self,parent,id,title,pos,size,style,name)

        self.panel = wx.ScrolledWindow(self,wx.ID_ANY)

        menuBar = wx.MenuBar()
        menu1 = wx.Menu()
        m = menu1.Append(wx.NewId(), "&Blah", "Show Pictures")
        menuBar.Append(menu1,"&Blah")
        self.Bind(wx.EVT_MENU,self.OnInit,m)

        self.SetMenuBar(menuBar)

    def OnInit(self, event):

        sizer = wx.GridSizer(rows=7,cols=3)

        filenames = []
        for i in range(20):
            filenames.append("img"+str(i)+".png")
        for fn in filenames:
            img = wx.Image(fn,wx.BITMAP_TYPE_ANY)
            sizer.Add(wx.StaticBitmap(self.panel,wx.ID_ANY,wx.BitmapFromImage(img)))

        self.panel.SetSizer(sizer)

class MyApp(wx.App):

    def OnInit(self):

        self.frame = MyFrame(parent=None,title="Frame")
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True

if __name__ == "__main__":

    app = MyApp()
    app.MainLoop()

回答1:


Insert this

self.panel.SetScrollbars(1, 1, 1, 1)

after self.panel = wx.ScrolledWindow(self,wx.ID_ANY)

If you want some info on the SetScrollBars method then look at this wxwidgets documentation page



来源:https://stackoverflow.com/questions/3392631/creating-scrolledwindow-in-wxpython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!