简体   繁体   中英

Unable to add checkbox to wxpython GUI during runtime

I am attempting to added checkboxes to a wxpython gui during runtime, but it does not seem to be showing up. My code is below.

I have tried following the post < Add checkbox in wxPython in runtime >, but was not able to get it to work. I also used wxFormBuilder to see how it adds a checkbox during initialization; I was able to verify that self.mainWindow.p_SelectionPanel is where I want to add the checkbox. I have also checked with the debugger to ensure that each line of the code is run at least once.

A little more background about the application: it is a wxPython GUI with a matplotlib plot embedded into it. I am trying to generate checkboxes from an incoming serial port stream so the user can show/hide series during runtime. point is a dictionary with the key as the series name and the series value as the dictionary value.

Please let me know if you need more context.

Thanks in advance for the help.

 def addNewCheckBoxes(self,point):
        sizer = self.mainWindow.p_SelectionPanel.GetSizer()
        addedCheckBox = False

        for key in point.keys():
            if key not in self.cbList.keys():
                self.cbList[key] = wx.CheckBox(self.mainWindow.p_SelectionPanel)
                sizer.Add(self.cbList[key])
                addedCheckBox = True

        if addedCheckBox:
            self.mainWindow.p_SelectionPanel.SetSizer(sizer)
            self.mainWindow.p_SelectionPanel.Layout()

This issue is cause by utilizing multiple threads; see comments above. I have been able to "hand off" the addition of check boxes to the main thread by using the techniques addressed here: < Sharing data between threads in Python >.

Though, a better, thread-safe way to structure my program is suggested here: < WxPython: Periodically set value in TextCtrl not working >. There is also a way that avoids the use of multiple threads noted, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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