2012-08-28 38 views
0

我使用Python 2.6與wxPython 2.8.12通過BoaConstructor開發應用程序。我有一個應用程序按鈕,它將存儲來自TextCtrl的一些變量,然後在對話框窗口上執行self.Destroy()調用來關閉它。當我點擊這個按鈕時,GUI掛起並且應用程序凍結。但是,如果我不調用self.Destroy(),我可以在沒有問題的情況下存儲變量,直到我嘗試使用窗口右上角的X按鈕(使用打印命令進行測試)關閉窗口。如果我不試圖存儲任何變量,只需按下按鈕執行self.Destroy(),一切都很好。只有當我嘗試存儲變量並執行GUI掛起的self.Destroy()時。任何機構都可以伸出援手嗎?提前致謝。wxPython對話self.Destoy()將凍結應用程序

def OnApplyButton(self, event): 
     print self.IMSINumberDigitTextCtrl.GetValue() 
     if self.IMSINumberDigitCheckBox.GetValue() == True: 
      print self.IMSINumberDigit #Debugging purposes only. Returns expected value 
      print self.IMSINumberDigitTextCtrl.GetValue() #Debugging purposes only. Returns expected value 
      self.IMSINumberDigit = self.IMSINumberDigitTextCtrl.GetValue() 
      print self.IMSINumberDigit #Debugging purposes only. Returns expected value 
    self.Destroy() 

編輯:這是指邁克的建議。這些是我測試的較小程序的兩個文件,其工作完美無瑕。

#!/usr/bin/env python 
#Boa:App:BoaApp 

import wx 

import Frame1 

modules = {'Frame1': [1, 'Main frame of Application', u'Frame1.py']} 

class BoaApp(wx.App): 
    def OnInit(self): 
     self.main = Frame1.create(None) 
     self.main.Show() 
     self.SetTopWindow(self.main) 
     return True 

def main(): 
    application = BoaApp(0) 
    application.MainLoop() 

if __name__ == '__main__': 
    main() 

而且

#Boa:Frame:Frame1 

import wx 

def create(parent): 
    return Frame1(parent) 

[wxID_FRAME1, wxID_FRAME1APPLY, wxID_FRAME1CHECKBOX1, wxID_FRAME1CHECKBOX2, 
wxID_FRAME1TEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(5)] 

class Frame1(wx.Frame): 
    def _init_ctrls(self, prnt): 
     # generated method, don't edit 
     wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt, 
       pos=wx.Point(480, 245), size=wx.Size(279, 135), 
       style=wx.DEFAULT_FRAME_STYLE, title='Frame1') 
     self.SetClientSize(wx.Size(279, 135)) 

     self.checkBox1 = wx.CheckBox(id=wxID_FRAME1CHECKBOX1, label='checkBox1', 
       name='checkBox1', parent=self, pos=wx.Point(24, 16), 
       size=wx.Size(95, 22), style=0) 
     self.checkBox1.Bind(wx.EVT_CHECKBOX, self.OnCheckBox1Checkbox, 
       id=wxID_FRAME1CHECKBOX1) 

     self.checkBox2 = wx.CheckBox(id=wxID_FRAME1CHECKBOX2, label='checkBox2', 
       name='checkBox2', parent=self, pos=wx.Point(48, 48), 
       size=wx.Size(95, 22), style=0) 
     self.checkBox2.Enable(False) 
     self.checkBox2.Bind(wx.EVT_CHECKBOX, self.OnCheckBox2Checkbox, 
       id=wxID_FRAME1CHECKBOX2) 

     self.Apply = wx.Button(id=wxID_FRAME1APPLY, label=u'Apply', 
       name=u'Apply', parent=self, pos=wx.Point(24, 88), 
       size=wx.Size(232, 29), style=0) 
     self.Apply.Bind(wx.EVT_BUTTON, self.OnApplyButton, id=wxID_FRAME1APPLY) 

     self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1', 
       parent=self, pos=wx.Point(160, 44), size=wx.Size(80, 27), style=0, 
       value='textCtrl1') 
     self.textCtrl1.SetEditable(True) 
     self.textCtrl1.Enable(False) 

    def __init__(self, parent): 
     self._init_ctrls(parent) 
     self.TextCtrlVariableHolder = None 

    def OnCheckBox1Checkbox(self, event): 
     value = self.checkBox1.GetValue() 
     if value == True: 
      self.checkBox2.Enable(True) 
     else: 
      self.checkBox2.Enable(False) 

    def OnCheckBox2Checkbox(self, event): 
     value = self.checkBox2.GetValue() 
     if value == True: 
      self.textCtrl1.Enable(True) 
     else: 
      self.textCtrl1.Enable(False) 

    def OnApplyButton(self, event): 
     print self.textCtrl1.GetValue() 
     if self.checkBox2.GetValue() == True: 
      print self.TextCtrlVariableHolder 
      print self.textCtrl1.GetValue() 
      self.TextCtrlVariableHolder = self.textCtrl1.GetValue() 
      print self.TextCtrlVariableHolder 
     self.Destroy() 

謝謝!

+0

你能提供一個小的可運行的這種行爲的例子嗎?你使用的是什麼wxPython版本和操作系統? –

+0

嘿@MikeDriscoll感謝您的回覆。我只是嘗試了你的建議,它沒有任何問題。然而,我真正的計劃中的問題仍然存在。我在CentOS 6.2上使用wxPython 2.8.12。這個失敗的程序有62個GUI組件。組件的剪切量是否會導致wxPython在嘗試關閉框架時失敗?下面的代碼以較小的尺寸複製較大的應用程序。 –

+0

不太可能。我的圖形用戶界面有更多的小部件。你在使用線程嗎?你要離開文件句柄嗎?你需要關閉數據庫連接嗎?你爲什麼要銷燬而不是關閉? –

回答

0

原來問題出在我自己的編碼上,我一直懷疑這個問題,但無法隔離。由於我捕獲值的方式,我有一個無限循環運行。 TextCtrls返回字符串,而不是ints。誰知道。所以在對話框產生的框架中,我<「StringThatIThoughtWasAnINT」永遠不會逃脫。感謝您的幫助,儘管@MikeDriscoll!