0

我是 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)
4

2 回答 2

2

I was able to get this working by putting in a global variable in the first two button handlers.

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
    ...

Then in the third button handler:

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

The third button handler then successfully calls the convert function.

于 2012-06-19T16:07:11.443 回答
0

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

def button_submitPressed(self, sender, args):
    convert(self.FILENAME,self.LAYOUT)
于 2014-09-03T16:36:35.340 回答