2012-07-16 42 views
2

我有一個按鈕,可以創建一個新的子框架或顯示已創建的框架。當我試圖單獨使用Show()時遇到了問題 - 如果用戶已退出子框架,則會因爲訪問不再存在的框架而出現錯誤。我目前正在使用try /除了解決這個問題,但有沒有更好的方法?也許一個類似Raise()的函數來處理這個問題,或者一種檢查框架是否存在的方法?顯示已刪除的子框架(wxPython)

代碼:

#!/usr/bin/env python 

import wx 

class LogWindow(wx.Frame): 
    def __init__(self, parent): 
     wx.Frame.__init__(self, parent) 
     self.logger = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_READONLY) 

    def Print(self): 
     self.Raise() 
     self.logger.AppendText("Hello, world\n") 

class MainWindow(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self, parent, id, title) 
     panel=wx.Panel(self) 

     label = wx.StaticText(panel, -1, "Log this message:", pos=(10,10)) 
     goButton = wx.Button(panel, label="Log", pos=(10,50)) 
     self.Bind(wx.EVT_BUTTON, self.OnClick, goButton) 
     self.logWin = LogWindow(self) 

    #++++++++++++++++++++++++++ 
    def OnClick(self, event): 
     try: 
      self.logWin.Show() 
     except: 
      self.logWin = LogWindow(self) 
      self.logWin.Show() 

     self.logWin.Print() 
    #++++++++++++++++++++++++++ 

class MyApp(wx.App): 
    def OnInit(self): 
     frame = MainWindow(None, -1, "MyApp") 
     frame.Show(True) 
     self.SetTopWindow(frame) 
     return True 

#************************************************ 

if __name__ == "__main__": 
    app = MyApp(0) 
    app.MainLoop() 

self.logWin.Show()收到沒有嘗試錯誤/除非是

wx._core.PyDeadObjectError: The C++ part of the LogWindow object has been deleted, attribute access no longer allowed.

回答