IronPython Windows表单,在python中传递变量

时间:2012-06-18 18:41:52

标签: python ironpython

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

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

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

前两个按钮工作正常。我的问题是将文件名传递给'button_submitPressed'函数。我试图制作FILENAME和LAYOUT全局变量(我已经在'HelloWorldForm'类的内部和外部尝试过,但两者都没有工作)。

如何将我在按钮事件中收集的变量传递给另一个函数?

当我运行它时,当我点击提交按钮(点击前两个并选择文件名和布局后)后,我收到错误:

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)

2 个答案:

答案 0 :(得分:2)

我能够通过在前两个按钮处理程序中放入一个全局变量来实现此功能。

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函数。

答案 1 :(得分:0)

对于未来的读者,OP的代码如果通过类访问他的类级别常量将会有效:

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