全局变量的麻烦

时间:2012-10-24 10:20:07

标签: python user-interface wxpython

当运行我的类的实例时,如下面的代码所示。我尝试使用print printWindow.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()

1 个答案:

答案 0 :(得分:2)

从技术上讲,它不会打印“无”,它会向stdout打印一个空行。

这是因为您使用空字符串初始化dataValues中的ImportDialog并且仅在confirmOK中更改该字符串,我猜是在用户按下确认按钮后。

在用户有机会按下该按钮之前,您的print语句会被执行。