Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 全局变量的问题_Python_User Interface_Wxpython - Fatal编程技术网

Python 全局变量的问题

Python 全局变量的问题,python,user-interface,wxpython,Python,User Interface,Wxpython,当运行我的类的实例时,如下面的代码所示。我尝试使用“print importWindow.GetDataVal()”来显示输入对话框的值,但它不打印任何内容。如何使其显示必要的值 import wx class ImportDialog(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: ImportDialog.__init__ kwds["style"] = wx.DE

当运行我的类的实例时,如下面的代码所示。我尝试使用“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\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()
导入wx
类导入对话框(wx.Frame):
定义初始值(self,*args,**kwds):
#开始wxGlade:ImportDialog.\uuu init__
kwds[“样式”]=wx.DEFAULT\u FRAME\u样式
wx.Frame.\uuuu init\uuuux(self,*args,**kwds)
self.dataValues=''
self.path=wx.TextCtrl(self,-1,“,size=(175,25))
self.button_1=wx.button(self,-1,“浏览haarcascades”)
self.button_1.Bind(wx.EVT_button,self.fetchHaarPath)
self.ok=wx.按钮(self,-1,“ok”)
self.ok.Bind(wx.EVT_按钮,self.confirmOK)
self.cancel=wx.按钮(self,-1,“取消”)
self.cancel.Bind(wx.EVT_按钮,self.CloseAll)
self.label_1=wx.StaticText(self,-1,“检测对象的名称:”)
self.name=wx.TextCtrl(self,-1,“”)
self.spin\u ctrl\u 1=wx.SpinCtrl(self,-1,“,最小值=0,最大值=100)
self.spin\u ctrl\u 2=wx.SpinCtrl(self,-1,“,最小值=0,最大值=100)
self.\uuuu set\u properties()
self.\u do\u布局()
#结束wxGlade
定义设置属性(自):
#开始wxGlade:ImportDialog.\u设置\u属性
self.SetTitle(“导入自定义Haarcascade”)
#结束wxGlade
定义布局(自):
#开始wxGlade:ImportDialog.\u do\u布局
grid_sizer_1=wx.GridSizer(3,2,0,0)
网格大小器1.添加((20,20),0,0,0)
网格大小器1.添加((20,20),0,0,0)
网格大小器1.Add(self.path,0,0,0)
网格大小器添加(self.button\u 1,0,0)
网格大小器1.添加((20,20),0,0,0)
网格大小器1.添加((20,20),0,0,0)
网格大小器添加(self.label\u 1,0,0)
网格大小器1.Add(self.name,0,0,0)
网格大小器1.添加((20,20),0,0,0)
网格大小器1.添加((20,20),0,0,0)
grid_sizer_1.Add(self.spin_ctrl_1,0,0)
网格大小器1.Add(self.spin\u ctrl\u 2,0,0)
网格大小器1.添加((20,20),0,0,0)
网格大小器1.添加((20,20),0,0,0)
网格大小器1.Add(self.ok,0,0,0)
网格大小器1.Add(self.cancel,0,0,0)
自我设置器(网格大小器1)
网格尺寸器1.适合(自身)
self.Layout()
#结束wxGlade
def fetchHaarPath(自身,事件):
wildcard=“XML文件(.XML)|*.XML”
haarPicker=wx.FileDialog(无,“选择文件”,
通配符=通配符,
样式=wx.OPEN)
如果haarPicker.showmodel()==wx.ID\u正常:
#打印“Haarcascade文件路径:”,haarPicker.GetPath()
self.path.SetValue(haarPicker.GetPath())
haarPath=haarPicker.GetPath()
haarPicker.Destroy()
self.haarPath=haarPath
def确认(自身、事件):
打印“路径:”,self.Path.GetValue()
打印“Name:,self.Name.GetValue()
打印“大小:”,self.spin\u ctrl\u 1.GetValue(),“X”,self.spin\u ctrl\u 2.GetValue()
打印
#C:\Users\Foster\Documents\Roland\ims project\haarcascades\eyegories.xml@(50,50)&&eye
#C:\Users\Foster\Documents\Roland\ims project\haarcascades\eyegories.xml@(50,50)&&eye
dataVal=“”
dataVal=str(self.path.GetValue())+“@(“+str(self.spin\u ctrl\u 1.GetValue())+”,“+str(self.spin\u ctrl\u 2.GetValue())+”&“+str(self.name.GetValue())
self.setDataVal(dataVal)
#打印self.GetDataVal()
self.CloseAll(事件)
def GetDataVal(自身):
返回self.dataValues
def setDataVal(自身,valueData):
self.dataValues=valueData
def CloseAll(自身、事件):
self.Close()
#课程结束导入对话框
如果名称=“\uuuuu main\uuuuuuuu”:
app=wx.PySimpleApp(0)
wx.InitAllImageHandlers()文件
importWindow=ImportDialog(无,-1,”)
应用程序SetTopWindow(导入窗口)
importWindow.Show()
打印“”
app.MainLoop()

从技术上讲,它不会打印“nothing”,而是打印一个空行到stdout

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


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

@SWAPYAutomation yes它会打印“我认为@Fabian是对的,请尝试使用
init
部分中的
init
部分打印:<>Path:“file Path”Name:“Name“尺寸:100 X 100你们说得对,它现在打印<测试>路径:名称:尺寸:0 X 0