【问题标题】:IronPython Windows forms, passing variables in pythonIronPython Windows 窗体,在 python 中传递变量
【发布时间】:2012-06-18 18:41:51
【问题描述】:

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

【问题讨论】:

    标签: python ironpython


    【解决方案1】:

    我可以通过在前两个按钮处理程序中放入一个全局变量来完成这项工作。

    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)
    

    然后第三个按钮处理程序成功调用转换函数。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-25
        • 1970-01-01
        • 1970-01-01
        • 2012-03-15
        • 1970-01-01
        相关资源
        最近更新 更多