2014-09-03 99 views
-1

我是wxPython的新手。我在文件中有多個記錄,每個記錄必須從文件中讀取並顯示在不同的幀中。我正嘗試在for循環中創建框架,但我希望在第一個框架被銷燬後才能創建第二個框架。以下是我的代碼:在循環中創建多個幀wxPython

import wx 
import textentry 

class Frame(wx.Frame): 
    def __init__(self, fargs, **kwargs): 
     wx.Frame.__init__(self, fargs, **kwargs) 
     #self.Bind(wx.EVT_CLOSE, self.OnClose) 
     self.panel = wx.Panel(self) 
     self.box = wx.BoxSizer(wx.VERTICAL) 

     self.m_text = wx.StaticText(self.panel, -1, label = suggested.lstrip()) 
     self.m_text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD)) 
     self.m_text.SetSize(self.m_text.GetBestSize()) 
     self.box.Add(self.m_text, 0, wx.ALL, 10) 


     self.filler_text = wx.StaticText(self.panel, -1, "************ Description ****************") 
     self.filler_text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD)) 
     self.filler_text.SetSize(self.filler_text.GetBestSize()) 
     self.box.Add(self.filler_text, 0, wx.ALL, 10) 



     self.d_text = wx.StaticText(self.panel, -1, label = description.lstrip()) 
     self.d_text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD)) 
     self.d_text.SetSize(self.d_text.GetBestSize()) 
     self.box.Add(self.d_text, 0, wx.ALL, 10) 

     self.m_close = wx.Button(self.panel, wx.ID_EDIT, "Edit") 
     self.m_close.Bind(wx.EVT_BUTTON, self.onReject) 
     self.box.Add(self.m_close, 0, wx.ALL, 10) 

     self.m_skip = wx.Button(self.panel, wx.ID_EDIT, "Skip") 
     self.m_skip.Bind(wx.EVT_BUTTON, self.onSkip) 
     self.box.Add(self.m_skip, 0, wx.ALL, 10) 

     self.m_accept = wx.Button(self.panel, wx.ID_YES, "Accept") 
     self.m_accept.Bind(wx.EVT_BUTTON, self.OnAccept) 
     self.box.Add(self.m_accept, 0, wx.ALL, 10) 
     self.box.SetSizeHints(self) 
     self.panel.SetSizer(self.box) 
     self.panel.Layout() 

    def onReject(self, event): 
     dialog = textentry.TextEntryDialog(None, 'Edit License Info', 'Enter License information') 
     dialog.Center() 
     dialog.SetValue(self.m_text.GetLabel()) 
     if dialog.ShowModal() == wx.ID_OK: 
      self.m_text.SetLabel(dialog.GetValue()) 
      self.box.SetSizeHints(self) 
      self.panel.Layout() 
      dialog.Destroy() 

    def onSkip(self, event): 
     self.Destroy() 

    def OnClose(self, event): 
     dlg = wx.MessageDialog(self,"Do you really want to close this application?","Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION) 
     result = dlg.ShowModal() 
     dlg.Destroy() 
     if result == wx.ID_OK: 
      self.Destroy() 

    def OnAccept(self, event): 
     f = open("AcceptedLicense.txt", "a") 
     print self.m_text.GetLabel().encode('utf-8') 
     f.write(self.m_text.GetLabel().encode('utf-8') + '\n') 
     f.close() 
     self.Destroy() 

panel ='' 
app = wx.App(False) 
f = open("License.txt", "r") 
main_message = f.read() 
f.close() 
if main_message != None: 
    for m in main_message.split('Suggested License Text:'): 
     if m != "": 
      desc = [d for d in m.split("Description:")] 
      suggested = desc[0] 
      description = desc[1] 
      top = Frame(None) 
      top.Show() 
app.MainLoop() 

This is the code I use to create multiple frames: 

if main_message != None: 
    for m in main_message.split('Suggested License Text:'): 
     if m != "": 
      desc = [d for d in m.split("Description:")] 
      suggested = desc[0] 
      description = desc[1] 
      top = Frame(None) 
      top.Show() 

任何幫助,非常感謝。

+0

您還沒有提出任何問題。如果代碼生成錯誤,則將其記錄下來。 SO不是代碼審查服務。 – user590028 2014-09-03 17:55:48

回答

1

您需要在框架顯示之前開始app.MainLoop()。在您的代碼中,所有框架都在MainLoop()運行之前創建,這就是框架全部一次顯示的原因。而不是創建多個框架,創建多個面板,只需隱藏以前的面板。

還要控制框架的創建,請嘗試返回一些東西,在事件處理函數調用之前調用destroy()。並在主框架中檢查返回以創建下一個面板。