0

当运行我的类的实例时,如下面的代码所示。我尝试使用“打印 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\eyeglasses.xml@@(50,50)&&eye
        #C:\Users\Foster\Documents\Roland\ims project\haarcascades\eyeglasses.xml@@(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()
4

1 回答 1

2

从技术上讲,它不会打印“无”,它会在标准输出中打印一个空行。

那是因为您使用空字符串进行初始化并且只更改该字符串dataValues,我猜是在用户按下确认按钮之后。ImportDialogconfirmOK

您的打印语句在用户有机会按下该按钮之前执行。

于 2012-10-24T10:31:25.610 回答