简体   繁体   English

wxPython面板未显示在Show()

[英]wxPython panel not showing at Show()

So I have a small app. 所以我有一个小应用。 The gist of it is just a window with a button in the center of the main frame. 要点只是一个在主框架中央带有按钮的窗口。 When you click the button, the button gets hidden and a gauge/loading bar appears in the center of the main frame and fills up as it does some computations( the filling up part is not implemented yet, at the moment it's just an empty gauge that sits there). 单击该按钮时,该按钮将隐藏,并且仪表盘/加载条出现在主机架的中心,并随着一些计算的进行填充(填充部分尚未实现,目前它只是一个空表那里)。

The button and the gauge are each implemented in their own panels, and to "switch" between them I just disable an hide the button panel and then enable and show the gauge panel. 按钮和仪表分别在各自的面板中实现,要在它们之间“切换”,我只需禁用“隐藏”按钮面板,然后启用并显示仪表面板即可。 To switch back I just reverse that process. 要切换回去,我只需要反转该过程即可。 Pretty straight forward. 非常简单。

Now, as a test I wanted the button, when clicked, to just switch to the gauge for a couple seconds and the switch back(using Sleep() ). 现在,作为测试,我希望单击按钮时,只需切换到压力表几秒钟,然后再切换回去(使用Sleep() )。

This is the relevant code(please ask for more if you consider it would be relevant): 这是相关的代码(请考虑是否需要更多代码):

import wx
import time
import gui_base


class MainFrame(gui_base.main_frame):
        def __init__(self):
                super(MainFrame, self).__init__(None)
                self.set_state_pre_compare()

        def set_state_pre_compare(self):
                self.panel_in_compare_panel.Disable()
                self.panel_in_compare_panel.Hide()
                print('done hiding in')
                self.panel_pre_compare_panel.Enable()
                self.panel_pre_compare_panel.Show()
                print('done enabling pre')
                self.Layout()

        def set_state_in_compare(self):
                self.panel_pre_compare_panel.Disable()
                self.panel_pre_compare_panel.Hide()
                print('done hiding pre')
                self.panel_in_compare_panel.Enable()
                self.panel_in_compare_panel.Show()
                print('done enabling in')
                self.Layout()

        def on_compare_button_click(self, event):
                self.set_state_in_compare()
                time.sleep(2.5)
                self.set_state_pre_compare()


if __name__ == '__main__':
    app = wx.App(True)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

The pre and in panels refer to "pre computation"(panel with the button, computations not yet started) and "in computation"(panel with the gauge, computations started) prein面板分别指“预计算”(带按钮的面板,尚未开始计算)和“ in computing”(带量规的面板,已开始计算)

My big problem is this: the app starts up fine, I click the button, the button disappears just fine, however the loading bar does not show at all, and after a few seconds the button shows back up. 我的大问题是:应用程序正常启动,我单击按钮,按钮消失得很好,但是加载栏根本不显示,几秒钟后按钮又显示回来。

I also used some print statements to check if the functions complete/hand somewhere but all seems great, nothing hangs, everything completes nicely. 我还使用了一些打印语句来检查功能是否在某处完成/传递,但是一切似乎都很好,没有任何挂起,一切都很好地完成了。

Also if I set the initial state of the app to be with the gauge showing(meaning there is no way to switch to the button) the gauge shows just fine. 另外,如果我将应用程序的初始状态设置为以仪表显示(表示无法切换到按钮),仪表显示就可以了。

The conclusion I get are that the GUI elements themselves are okay since at startup, if i start the program with either of the 2 states as the initial one, that state shows up just fine. 我得到的结论是,GUI元素本身还不错,因为在启动时,如果我以2个状态中的任何一个作为初始状态启动程序,则该状态显示就很好。

Also that the switching function, to some degree works, although I am not sure to what extent. 虽然我不确定到什么程度,但该切换功能在某种程度上还是有效的。

May bottom question is: why does it not switch between the states nicely if both panels show up just fine if they are set as the startup panel? 可能的问题是:如果将两个面板都设置为启动面板,那么如果两个面板都显示得很好,为什么它不能很好地在状态之间切换? And what would be the fix? 那么解决办法是什么?

Thank you in advance! 先感谢您!

EDIT: 编辑:

I modified the code like this: 我修改了这样的代码:

 def on_compare_button_click(self, event):
                event.Skip()
                self.set_state_in_compare()
                self.delay()


 def delay(self):
                for i in range(1500):
                        print(i)

To simulate some delay and what I noticed is that the frame contents only really update at the end of that function. 为了模拟一些延迟,我注意到帧内容仅在该函数结束时才真正更新。 If I don't switch back to the button panel, the gauge will work just fine, but on pressing the button, the button panel gets hidden, the show/enable for the gauge gets called and completes, but the gauge will not show up(the whole frame will be a blank window) until the delay function is over. 如果我不切换回按钮面板,仪表将正常工作,但是按下按钮时,按钮面板将被隐藏,仪表的显示/启用被调用并完成,但是仪表将不会显示(整个帧将是一个空白窗口),直到延迟功能结束。 I have tried putting set_state or delay in their own threads, or both in his own thread, nut nothing seems to change what happens. 我尝试将set_statedelay放在自己的线程中,或将两者都放在自己的线程中,但似乎没有什么改变。

Why does this happen? 为什么会这样? And how can I make the GUI actually update when it is meant to(when the set_state functions get called)? 以及如何在需要时(当调用set_state函数时)实际更新GUI?

I figured it out. 我想到了。 From what I can understand, the GUI only gets updated when the function completed. 据我了解,GUI仅在功能完成时才更新。 To force a GUI update, calling self.Layout() and self.Update() does the trick. 要强制进行GUI更新,请调用self.Layout()self.Update()问题。

Leaving this here in case someone might stumble into the same thing. 留在这里,以防有人偶然发现同一件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM