IronPython Windows窗体,在python中传递变量

IronPython Windows窗体,在python中传递变量,python,ironpython,Python,Ironpython,我是IronPython新手,尝试使用windows窗体创建一个简单的应用程序,将固定字段文件转换为分隔文件 我创建了一个有三个按钮的表单 第一个是选择要转换的文件。 第二种方法是选择具有第一个文件布局的文件。 第三个是“提交”按钮,用于将上面两个文件的文件名发送到将转换文件的python函数 前两个按钮工作正常。我的问题是将文件名传递给“button_submitPressed”函数。我尝试创建文件名和布局全局变量(我在“HelloWorldForm”类内外都尝试过,但都不起作用) 我必须做什

我是IronPython新手,尝试使用windows窗体创建一个简单的应用程序,将固定字段文件转换为分隔文件

我创建了一个有三个按钮的表单

第一个是选择要转换的文件。 第二种方法是选择具有第一个文件布局的文件。 第三个是“提交”按钮,用于将上面两个文件的文件名发送到将转换文件的python函数

前两个按钮工作正常。我的问题是将文件名传递给“button_submitPressed”函数。我尝试创建文件名和布局全局变量(我在“HelloWorldForm”类内外都尝试过,但都不起作用)

我必须做什么才能将在按钮事件中收集的变量传递给另一个函数

当我运行此操作时,当我单击submit按钮(在单击前两个并选择文件名和布局后)时,我得到错误:

IronPython.Runtime.UnboundNameException: global name 'FILENAME' is not defined
谢谢

class HelloWorldForm(Form):
    FILENAME = ''
    LAYOUT = ''
    def __init__(self):
        self.Text = 'ff2delim'

        self.label = Label()
        self.label.Text = "Convert fixed legnth file to delimited"
        self.label.Location = Point(50, 50)
        self.label.Height = 30
        self.label.Width = 200

        self.count = 0

        button = Button()
        button.Text = "File name"
        button.Location = Point(50, 100)

        button.Click += self.buttonPressed

        button2 = Button()
        button2.Text = "Layout"
        button2.Location = Point(50, 130)

        button2.Click += self.button2Pressed

        button_submit = Button()
        button_submit.Text = "Convert"
        button_submit.Location = Point(50, 190)

        button_submit.Click += self.button_submitPressed

        self.Controls.Add(self.label)
        self.Controls.Add(button)
        self.Controls.Add(button2)
        self.Controls.Add(button_submit)


    def buttonPressed(self, sender, args):
        dialogf = OpenFileDialog()
        if dialogf.ShowDialog() == DialogResult.OK:
            FILENAME = dialogf.FileName
            print "FILENAME: " + FILENAME 
            self.label_filename = Label()
            self.label_filename.Text = FILENAME
            self.label_filename.Location = Point(140, 105)
            self.label_filename.Height = 30
            self.label_filename.Width = 200    
            self.Controls.Add(self.label_filename) 
        else:
            print "No file selected"

    def button2Pressed(self, sender, args):
        dialogl = OpenFileDialog()
        if dialogl.ShowDialog() == DialogResult.OK:
            LAYOUT = dialogl.FileName
            print "LAYOUT: " + LAYOUT 
            self.label_layout = Label()
            self.label_layout.Text = LAYOUT
            self.label_layout.Location = Point(140, 135)
            self.label_layout.Height = 30
            self.label_layout.Width = 200    
            self.Controls.Add(self.label_layout) 
        else:
            print "No file selected"

    def button_submitPressed(self, sender, args):
        convert(FILENAME,LAYOUT)

我能够通过在前两个按钮处理程序中加入一个全局变量来实现这一点

def buttonPressed(self, sender, args):
    global FILENAME
    dialogf = OpenFileDialog()
    if dialogf.ShowDialog() == DialogResult.OK:
        FILENAME = dialogf.FileName
        print "FILENAME: ",FILENAME
    ...

def button2Pressed(self, sender, args):
    global LAYOUT
    dialogl = OpenFileDialog()
    if dialogl.ShowDialog() == DialogResult.OK:
        LAYOUT = dialogl.FileName
        print "LAYOUT: " + dialogl.FileName
    ...
然后在第三个按钮处理程序中:

def button_submitPressed(self, sender, args):
    print "FILENAME SUB: ",FILENAME
    print "LAYOUT SUB: ",LAYOUT
    convert(FILENAME,LAYOUT)

然后,第三个按钮处理程序成功调用convert函数

对于未来的读者,如果OP通过类访问他的类级常量,那么他的代码就可以工作了:

def button_submitPressed(self, sender, args):
    convert(self.FILENAME,self.LAYOUT)