使用 wxPython 滚动窗口

发布于 2024-12-20 15:15:02 字数 2467 浏览 0 评论 0原文

我正在制作一个带有滚动条的框架,里面有一些图像。当框架为空时,滚动条工作正常。但是,当我添加图片时,滚动条似乎被推到框架的左上角。如何实现我的代码,以便在添加图片后滚动条保持在原来的位置?

工作守则;

    import wx
    import wx.animate


    class ScrollbarFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, -1, 'Scrollbar Example', pos = (100, 50), size=(1000, 1000))
            self.scroll = wx.ScrolledWindow(self, -1)
            self.scroll.SetScrollbars(1, 1, 1000, 1000)
            #self.button = wx.Button(self.scroll, -1, "Scroll Me", pos=(50, 20))
            #self.Bind(wx.EVT_BUTTON,  self.OnClickTop, self.button)
            #self.button2 = wx.Button(self.scroll, -1, "Scroll Back", pos=(500, 350))
            #self.Bind(wx.EVT_BUTTON, self.OnClickBottom, self.button2)
            self.SetBackgroundColour("gray")
            imageName = "01 background.png"
            gifName = "Jill.gif"
            backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
            gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(160, 74))
            # clears the background
            gifImage.GetPlayer().UseBackgroundColour(True)
            gifImage.Play()

    def update(self, imageName, gifName):
        backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
        gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(100, 100))
        # clears the background
        gifImage.GetPlayer().UseBackgroundColour(True)
        gifImage.Play()

    def OnClickTop(self, event):
        self.scroll.Scroll(600, 400)

    def OnClickBottom(self, event):
        self.scroll.Scroll(1, 1)


app = wx.PySimpleApp()
frame = ScrollbarFrame()
frame.Show()
app.MainLoop()

如果您注释掉这部分:

 gifName = "Jill.gif"
        backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
        gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(160, 74))
        # clears the background
        gifImage.GetPlayer().UseBackgroundColour(True)
        gifImage.Play()

窗口将与滚动条一起正确显示。但包含其中一个(或两个)图像文件时,就会出现问题。

I am making a frame with a scrollbar and some images inside. The scrollbar works fine when the frame is empty. However, when I add a picture in, the scrollbars seem to get pushed up into the top left corner of the frame. How can I implement my code so that the scrollbars stay where they are after I add pictures?

Working Code;

    import wx
    import wx.animate


    class ScrollbarFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, -1, 'Scrollbar Example', pos = (100, 50), size=(1000, 1000))
            self.scroll = wx.ScrolledWindow(self, -1)
            self.scroll.SetScrollbars(1, 1, 1000, 1000)
            #self.button = wx.Button(self.scroll, -1, "Scroll Me", pos=(50, 20))
            #self.Bind(wx.EVT_BUTTON,  self.OnClickTop, self.button)
            #self.button2 = wx.Button(self.scroll, -1, "Scroll Back", pos=(500, 350))
            #self.Bind(wx.EVT_BUTTON, self.OnClickBottom, self.button2)
            self.SetBackgroundColour("gray")
            imageName = "01 background.png"
            gifName = "Jill.gif"
            backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
            gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(160, 74))
            # clears the background
            gifImage.GetPlayer().UseBackgroundColour(True)
            gifImage.Play()

    def update(self, imageName, gifName):
        backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
        gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(100, 100))
        # clears the background
        gifImage.GetPlayer().UseBackgroundColour(True)
        gifImage.Play()

    def OnClickTop(self, event):
        self.scroll.Scroll(600, 400)

    def OnClickBottom(self, event):
        self.scroll.Scroll(1, 1)


app = wx.PySimpleApp()
frame = ScrollbarFrame()
frame.Show()
app.MainLoop()

if you comment out this part:

 gifName = "Jill.gif"
        backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
        gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(160, 74))
        # clears the background
        gifImage.GetPlayer().UseBackgroundColour(True)
        gifImage.Play()

the window displays properly with the scrollbar. But include either (or both) of the image files, and the problem occurs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

北座城市 2024-12-27 15:15:02

如果您希望图像位于滚动窗口面板内,则必须将静态 bipmap 和 gifImage 放入其中。因此,图像的父级不应该是 selfwx.Frame 实例),而是 self.scroll

修改所示的 4 行:

    ...................
    wx.StaticBitmap(self.scroll, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))    # <- this one
    gifImage = wx.animate.GIFAnimationCtrl(self.scroll, 0, gifName, pos=(160, 74))  # <- this one
    # clears the background
    gifImage.GetPlayer().UseBackgroundColour(True)
    gifImage.Play()

def update(self, imageName, gifName):
    backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
    wx.StaticBitmap(self.scroll, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))    # <- this one
    gifImage = wx.animate.GIFAnimationCtrl(self.scroll, 0, gifName, pos=(100, 100))  # <- this one
    ...................

这会将您的两个图像放在一起。如果您想将它们分开放置(列或行),那么您应该将它们添加到插入滚动窗口的大小调整器中

If you want your images inside the scrolled window panel, then you have to put your static bipmap and gifImage inside it. So the parent of your images should not be self (the wx.Frame instance) but self.scroll.

Modify the 4 lines indicated:

    ...................
    wx.StaticBitmap(self.scroll, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))    # <- this one
    gifImage = wx.animate.GIFAnimationCtrl(self.scroll, 0, gifName, pos=(160, 74))  # <- this one
    # clears the background
    gifImage.GetPlayer().UseBackgroundColour(True)
    gifImage.Play()

def update(self, imageName, gifName):
    backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
    wx.StaticBitmap(self.scroll, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))    # <- this one
    gifImage = wx.animate.GIFAnimationCtrl(self.scroll, 0, gifName, pos=(100, 100))  # <- this one
    ...................

This puts your two images one over the other. If you want to put them separately (column or row), then you should add them to a sizer inserted in your scrolled window

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