2012-10-24 126 views
0

當我的類的一個實例運行時,如下面的代碼所示。我嘗試使用'print importWindow.GetDataVal()'來顯示輸入到對話框中的值,但它不打印任何內容。我怎樣才能讓它顯示出必要的價值?全局變量故障

import wx 



class ImportDialog(wx.Frame): 


    def __init__(self, *args, **kwds): 
     # begin wxGlade: ImportDialog.__init__ 
     kwds["style"] = wx.DEFAULT_FRAME_STYLE 
     wx.Frame.__init__(self, *args, **kwds) 

     self.dataValues='' 
     self.path = wx.TextCtrl(self, -1, "", size=(175,25)) 
     self.button_1 = wx.Button(self, -1, "Browse for haarcascades") 
     self.button_1.Bind(wx.EVT_BUTTON, self.fetchHaarPath) 
     self.ok = wx.Button(self, -1, "OK") 
     self.ok.Bind(wx.EVT_BUTTON, self.confirmOK) 
     self.cancel = wx.Button(self, -1, "Cancel") 
     self.cancel.Bind(wx.EVT_BUTTON, self.CloseAll) 
     self.label_1 = wx.StaticText(self, -1, "Name of detect object:") 
     self.name = wx.TextCtrl(self, -1, "") 
     self.spin_ctrl_1 = wx.SpinCtrl(self, -1, "", min=0, max=100) 
     self.spin_ctrl_2 = wx.SpinCtrl(self, -1, "", min=0, max=100) 

     self.__set_properties() 
     self.__do_layout() 
     # end wxGlade 

    def __set_properties(self): 
     # begin wxGlade: ImportDialog.__set_properties 
     self.SetTitle("Import Custom Haarcascade") 
     # end wxGlade 

    def __do_layout(self): 
     # begin wxGlade: ImportDialog.__do_layout 

     grid_sizer_1 = wx.GridSizer(3, 2, 0, 0) 
     grid_sizer_1.Add((20, 20), 0, 0, 0) 
     grid_sizer_1.Add((20, 20), 0, 0, 0) 
     grid_sizer_1.Add(self.path, 0, 0, 0) 
     grid_sizer_1.Add(self.button_1, 0, 0, 0) 
     grid_sizer_1.Add((20, 20), 0, 0, 0) 
     grid_sizer_1.Add((20, 20), 0, 0, 0) 
     grid_sizer_1.Add(self.label_1, 0, 0, 0) 


     grid_sizer_1.Add(self.name, 0, 0, 0) 
     grid_sizer_1.Add((20, 20), 0, 0, 0) 
     grid_sizer_1.Add((20, 20), 0, 0, 0) 
     grid_sizer_1.Add(self.spin_ctrl_1, 0, 0, 0) 
     grid_sizer_1.Add(self.spin_ctrl_2, 0, 0, 0) 
     grid_sizer_1.Add((20, 20), 0, 0, 0) 
     grid_sizer_1.Add((20, 20), 0, 0, 0) 
     grid_sizer_1.Add(self.ok, 0, 0, 0) 
     grid_sizer_1.Add(self.cancel, 0, 0, 0) 
     self.SetSizer(grid_sizer_1) 
     grid_sizer_1.Fit(self) 
     self.Layout() 
     # end wxGlade 

    def fetchHaarPath(self,event): 
     wildcard = "XML files (.xml)|*.xml" 
     haarPicker = wx.FileDialog(None, "Choose a file", 
           wildcard=wildcard, 
           style=wx.OPEN) 
     if haarPicker.ShowModal() == wx.ID_OK: 
      #print "Haarcascade filepath:", haarPicker.GetPath() 
      self.path.SetValue(haarPicker.GetPath()) 
      haarPath=haarPicker.GetPath() 
     haarPicker.Destroy() 
     self.haarPath=haarPath 


    def confirmOK(self,event): 
     print "Path:", self.path.GetValue() 
     print "Name:", self.name.GetValue() 
     print "Size:", self.spin_ctrl_1.GetValue(),"X",self.spin_ctrl_2.GetValue() 


     print 
     #C:\Users\Foster\Documents\Roland\ims project\haarcascades\[email protected]@(50,50)&&eye 
     #C:\Users\Foster\Documents\Roland\ims project\haarcascades\[email protected]@(50,50)&&eye 
     dataVal="" 
     dataVal=str(self.path.GetValue())+"@@("+str(self.spin_ctrl_1.GetValue())+","+str(self.spin_ctrl_2.GetValue())+")&&"+str(self.name.GetValue()) 
     self.setDataVal(dataVal) 
     #print self.GetDataVal() 
     self.CloseAll(event) 

    def GetDataVal(self): 

     return self.dataValues 

    def setDataVal(self,valueData): 
     self.dataValues=valueData 





    def CloseAll(self,event): 

     self.Close() 


# end of class ImportDialog 
if __name__ == "__main__": 
    app = wx.PySimpleApp(0) 
    wx.InitAllImageHandlers() 
    importWindow = ImportDialog(None, -1, "") 
    app.SetTopWindow(importWindow) 
    importWindow.Show() 
    print "<" 
    print importWindow.GetDataVal() 
    print ">" 
    app.MainLoop() 
+0

它打印'print「<」'? – SWAPYAutomation

+0

@SWAPYAutomation是它打印「<」 – user1401950

+0

我認爲@Fabian是正確的,嘗試'init'部分 – SWAPYAutomation

回答

2

從技術上講,它不打印「無」,它打印出一個空行到標準輸出。

這是因爲你初始化dataValuesImportDialog與一個空字符串,並且只改變那個字符串confirmOK,我想在用戶按下確認按鈕後。

在用戶有機會按下該按鈕之前,您的打印語句被執行。