wx.python中滚动条消失/刷新无效

2 投票
1 回答
748 浏览
提问于 2025-04-28 07:10

在下面这个例子中,使用计算按钮后,滚动条消失了,尽管布局函数已经被调用。如果你手动调整窗口的大小,滚动条又会出现。这种情况只在Windows系统下发生,在Linux系统中,滚动条的表现正常。

为了修复这个问题,我尝试了refresh()和update()这两个函数(在GUI_Diagrams_GHL类的布局函数中)——但没有效果。

我试着把我的应用程序简化到这个最小可工作示例:

# -*- coding: utf-8 -*-

import wx
from wx.lib.pubsub import pub
import wx.lib.scrolledpanel as scrolled
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas

class MainWindow(wx.Frame):
    '''Frame that contains pretty much everything'''
    def __init__(self,*args,**kwargs):
        '''Constructor'''
        super(MainWindow,self).__init__(*args,**kwargs)
        self.panel = wx.Panel(self)
        notebook = Notebook(self.panel)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook,1, wx.ALL|wx.EXPAND,4)
        self.panel.SetSizerAndFit(sizer)
        self.panel.Layout()

class Notebook(wx.Notebook):
    def __init__(self, parent):
        wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style = wx.BK_DEFAULT)
        tabTwo = GUI_Input_GHL(self) 
        self.AddPage(tabTwo, 'Input')
        tabThree = GUI_Diagrams_GHL(self)  
        self.AddPage(tabThree, 'Diagrams')

class GUI_Input_GHL(scrolled.ScrolledPanel):
    """This panel contains the input fields for basic data."""
    def __init__(self, parent):
        scrolled.ScrolledPanel.__init__(self, parent=parent, id=wx.ID_ANY)
        self.label_1 = wx.StaticText(self,-1,label=u'Label 1')
        self.button2 = wx.Button(self,-1,label=u'Start')
        self.Bind(wx.EVT_BUTTON, self.StartCalc, self.button2)
        self.layout()       

    def layout(self):    
        sizer = wx.GridBagSizer()
        sizer.Add(self.button2, (8,0),(2,3), flag =wx.EXPAND)
        sizer.Add(self.label_1, (0,0),flag=wx.ALIGN_CENTER_VERTICAL)
        self.SetAutoLayout(1)
        self.SetupScrolling()
        self.SetSizerAndFit(sizer)

    def StartCalc(self,event):
        pub.sendMessage('GUI_Diagrams_Listener', message = 'test')

class GUI_Diagrams_GHL(scrolled.ScrolledPanel):
    """This panel contains diagrams"""
    def __init__(self, parent):
        scrolled.ScrolledPanel.__init__(self, parent=parent, id=wx.ID_ANY)
        self.parent = parent
        self.fig1 = Figure()
        self.fig6 = Figure()
        self.canvas1 = FigCanvas(self,-1,self.fig1)
        self.axes1 = self.fig1.add_subplot(111)
        self.canvas6 = FigCanvas(self,-1,self.fig6)
        self.axes6 = self.fig6.add_subplot(111)
        self.dia_R_hat_SetValues('test')
        self.dia_theta_SetValues('test')
        self.layout()
        pub.subscribe(self.diagrams_SetValues, "GUI_Diagrams_Listener")


    def layout(self):
        sizer = wx.GridBagSizer()
        sizer.Add(self.canvas1,   (1,0), (12,12), wx.EXPAND)
        sizer.Add(self.canvas6,  (53,0), (12,12), wx.EXPAND)

        ## I guess here is the problem somewhere:
        self.SetSizerAndFit(sizer)
        self.SetAutoLayout(1)
        self.SetupScrolling()
        #self.Fit()
        #self.Layout()
        #self.FitInside()
        #self.AlwaysShowScrollbars(True,True)
        #self.Refresh()
        #self.Update()
        #self.parent.SetSize(self.parent.GetSize())

    def diagrams_SetValues(self, message):
        self.Output = message
        self.dia_R_hat_SetValues(message)
        self.dia_theta_SetValues(message)
        self.layout()

    def dia_R_hat_SetValues(self, Output):
        self.axes1.clear()
        self.axes1.plot(range(15),range(15), 'r-', linewidth = 2)
        self.canvas1.draw()

    def dia_theta_SetValues(self, Output):
        self.axes6.clear()
        self.axes6.plot(range(5),'k')
        self.axes6.set_title(r"Absolute Temperature")
        self.canvas6.draw()


if __name__ == '__main__':
    app = wx.App()
    frame = MainWindow(None, -1, 'MyApp')
    frame.Show()
    app.MainLoop()
暂无标签

1 个回答

4

我自己搞定了 :)

如果在更新图表或者图表类的布局后,调用主面板的布局函数('MainWindow.panel.Layout()'),整个面板就会刷新,滚动条也会重新出现。所以在这种情况下,'parent.parent.Layout()'对我来说是有效的。

还有其他建议或解决方案吗?

撰写回答