禁用滚动条的滚动同时保持它在 wxPython 中可见 [英] Disablling scrolling of a scroll bar while keeping it visible in wxPython

查看:60
本文介绍了禁用滚动条的滚动同时保持它在 wxPython 中可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 Windows 7 操作系统上使用 wxPython v3.0、python v2.7.我对滚动条有疑问.在我的应用程序中,我有一个 GUI,它有许多滚动面板.这些滚动面板的滚动条也工作正常.

I am currently working with wxPython v3.0, python v2.7 on Windows 7 OS. I have question regarding the scroll bars. In my application I have a GUI which has many scrolled panels. The scroll bar of these scrolled panels are working fine too.

问题:我的问题是是否可以禁用滚动条的滚动,同时在特定的滚动面板上保持它可见?单击滚动按钮或拖动滚动条时滚动条不应滚动.

Problem: My question is that is it possible to disable the scrolling of a scroll bar while keeping it visible on a particular scrolled panel? The scroll bar shouldn't scroll when being clicked on the scroll buttons or by dragging the scroll bar.

我知道我们可以通过使用SetupScrolling(scroll_y=False)SetupScrolling(scroll_x=False) 来禁用水平或垂直滚动​​.但这也会使滚动条不可见.我还尝试使用 rate_y & 将滚动率设置为 0rate_x,这也使滚动条不可见.简而言之,我想显示滚动条但不执行任何操作.

I know that we can disable horizontal or vertical scrolling by using SetupScrolling(scroll_y=False) and SetupScrolling(scroll_x=False). But this will also make the scroll bar invisible. I also tried setting the scroll rate to 0 by using rate_y & rate_x, this also makes the scroll bar invisible. In short I want to show the scroll bar but make it do nothing.

有没有办法捕获滚动事件并使它们什么都不做?我通过将 myPanelwx 绑定,尝试了一种命中和试用方法.EVT_SCROLLWIN 来执行一个什么都不做的函数.不幸的是它没有工作(滚动条仍在滚动).

Is there a way to capture the scroll events and make them to do nothing? I tried a hit and trial method by binding myPanel with wx.EVT_SCROLLWIN to execute a function that does nothing. Unfortunately it didn't worked (the scroll bar is still scrolling).

myPanel = wx.lib.scrolledpanel.ScrolledPanel(self, -1, style=wx.SIMPLE_BORDER)
myPanel.Bind(wx.EVT_SCROLLWIN, self.onScroll)

def onScroll(self, event):
    pass

这是一个快速&脏代码示例可以玩,也可以下载!以避免识别错误.:

Here is a quick & dirty code sample to play around and can be downloaded too! to avoid identation errors.:

#!/usr/bin/env python

import wx
import wx.lib.scrolledpanel


class GUI(wx.Frame):

     def __init__(self, parent, id, title):
         screenSize = (400, 400)
         wx.Frame.__init__(self, None, id, title, size=screenSize)
         myFont = wx.Font(15, wx.MODERN, wx.NORMAL, wx.BOLD)
         panelsSizer = wx.BoxSizer(wx.VERTICAL)
         sizer1 = wx.BoxSizer(wx.VERTICAL)
         myPanel = wx.lib.scrolledpanel.ScrolledPanel(self, -1,style=wx.SIMPLE_BORDER)
         myPanel.Bind(wx.EVT_SCROLLWIN, self.onScroll)
         myPanel.SetupScrolling()
         panel1 = wx.lib.scrolledpanel.ScrolledPanel(myPanel, -1, style=wx.SIMPLE_BORDER)
         panel1.SetBackgroundColour('#FFFFFF')
         panel2 = wx.lib.scrolledpanel.ScrolledPanel(myPanel, -1, style=wx.SIMPLE_BORDER)
         panel2.SetBackgroundColour('#55F4FF')


         k = 0
         for i in range(1,7):
             sPanel ='Panel' +str(k)
             sPanel = wx.Panel(panel1)
             label = str(k)+'This is panel-1'
             text = wx.StaticText(sPanel, -1, label)
             text.SetForegroundColour('#0101DF')
             text.SetFont(myFont)
             sizer1.Add(sPanel, 0, wx.ALL, 5)
             sizer1.Add(wx.StaticLine(panel1), 0, wx.ALL|wx.EXPAND, 0)
             k += 1

         panel1.SetSizer(sizer1)
         panelsSizer.Add(panel1, 1, wx.EXPAND)
         panelsSizer.Add(panel2, 1, wx.EXPAND)
         myPanel.SetSizer(panelsSizer)

     def onScroll(self, event):
            pass


if __name__=='__main__':
    app = wx.App()
    frame = GUI(parent=None, id=-1, title="Test")
    frame.Show()
    app.MainLoop()

有什么建议吗?感谢您抽出宝贵时间.

Any suggestion? Thank you for your time.

推荐答案

我终于找到了解决问题的方法.但我不太明白它为什么起作用.:P

I finally figured out a solution to my problem. But I didn't quite understand that why it works. :P

我正在阅读关于 http://wiki.wxpython.org/AnotherTutorial#Events 的教程并发现了一些有趣的东西.维基指出:

I was reading tutorial on http://wiki.wxpython.org/AnotherTutorial#Events and found something interesting. The wiki states:

wx.ScrollWinEvent 事件产生,当我们点击一​​个内置的滚动条.使用 SetScrollbar() 激活内置滚动条方法调用.对于独立的滚动条,还有另一种事件类型,即 wx.ScrollEvent.

wx.ScrollWinEvent event is generated, when we click on a built in Scrollbar. Built-in Scrollbar is activated with the SetScrollbar() method call. For stand-alone Scrollbars, there is another event type, namely wx.ScrollEvent.

所以我决定设置一个内置滚动条,然后我将绑定我的 onScroll() 在滚动条移动时不做任何事情.现在我创建了一个简单的面板,而不是创建一个 scrolledPanel.

So I decided to setup a Built-in scroll bar and then I'll bind my onScroll() to do nothing when scroll bar is moved. Now instead of creating a scrolledPanel I created a simple Panel.

myPanel = wx.Panel(self, -1,style=wx.SIMPLE_BORDER)

然后我使用 setScrollbar()<创建一个内置滚动条/a>.

Then I create a Built-in scroll bar using setScrollbar().

myPanel.SetScrollbar(wx.VERTICAL, 0, 0, 2, 0)

然后我将 onScroll() 绑定到任何 滚动事件正在发生.

Then I bind the onScroll() to the any scrolling events that are happening.

myPanel.Bind(wx.EVT_SCROLLWIN, self.OnScroll)

所以,现在您会注意到 myPanel 上的滚动条在被单击或拖动时不会执行任何操作.

So, now you'll notice that the scroll bar on myPanel will do nothing on being clicked or dragged.

我不明白:

你真的不需要拦截任何滚动事件.您不需要绑定 onScroll().即使你没有绑定 onScroll(),滚动条仍然没有响应任何点击或拖动! 也许我缺少一些基本的东西.

You really don't need to intercept any scrolling events. You don't need to bind the onScroll(). Even if you don't bind the onScroll(), the scroll bar is still not responding to any clicking or dragging! Perhaps I am missing something basic.

这里是更新后的代码,也可用于下载以避免缩进问题:

Here is the updated code to play around also available for download to avoid indentation problems:

class GUI(wx.Frame):

    def __init__(self, parent, id, title):
        screenSize = (400, 400)
        wx.Frame.__init__(self, None, id, title, size=screenSize)
        myFont = wx.Font(15, wx.MODERN, wx.NORMAL, wx.BOLD)
        panelsSizer = wx.BoxSizer(wx.VERTICAL)
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        myPanel = wx.Panel(self, -1,style=wx.SIMPLE_BORDER)
        #Uncommenting the following line has no effect on scroll bar behavior!
        #myPanel.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
        myPanel.SetScrollbar(wx.VERTICAL, 0, 0, 2, 0)
        panel1 = wx.lib.scrolledpanel.ScrolledPanel(myPanel, -1, style=wx.SIMPLE_BORDER)
        panel1.SetBackgroundColour('#FFFFFF')
        panel2 = wx.lib.scrolledpanel.ScrolledPanel(myPanel, -1, style=wx.SIMPLE_BORDER)
        panel2.SetBackgroundColour('#55F4FF')


        k = 0
        for i in range(1,10):
            sPanel ='Panel' +str(k)
            sPanel = wx.Panel(panel1)
            label = str(k)+'This is panel-1'
            text = wx.StaticText(sPanel, -1, label)
            text.SetForegroundColour('#0101DF')
            text.SetFont(myFont)
            sizer1.Add(sPanel, 0, wx.ALL, 5)
            sizer1.Add(wx.StaticLine(panel1), 0, wx.ALL|wx.EXPAND, 0)
            k += 1

        panel1.SetSizer(sizer1)
        panelsSizer.Add(panel1, 1, wx.EXPAND)
        panelsSizer.Add(panel2, 1, wx.EXPAND)
        myPanel.SetSizer(panelsSizer)

    def OnScroll(self, evt):
          print "Got it"
          pass



if __name__=='__main__':
   app = wx.App()
   frame = GUI(parent=None, id=-1, title="Test")
   frame.Show()
   app.MainLoop()

这篇关于禁用滚动条的滚动同时保持它在 wxPython 中可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆