IronPython单击模块中的事件触发过程

时间:2018-03-26 15:30:43

标签: events module click ironpython

尝试将模块中的过程附加到TextChanged事件时出错。 enter image description here

附加事件代码:

import NewMortgage
txtNewMortgageValue = LogicalTreeHelper.FindLogicalNode(_tikitSender,'txtLoanAmount')
txtNewMortgageValue.TextChanged += NewMortgage.evtMortgage_RecalcNet

模块NewMortgage.py:

def evtMortgage_RecalcNet(sender, e):
   MessageBox.Show('test')

1 个答案:

答案 0 :(得分:1)

我将LogicalTreeHelper功能移动到模块中,然后可以访问事件代码。

我还创建了一个Global模块来保存经常使用的变量。

我首先包含模块

 import leanGlobal
 import leanNewMortgage

以下是leanNewMortgage模块,即在文件底部创建的类实例。

请注意,此模块还具有“import leanGlobal”,因此我可以访问全局变量和函数。

###### NEW MORTGAGE FUNCTIONS & VARS ###### 
from System.Windows import MessageBox
from System.Windows import LogicalTreeHelper
import leanGlobal

class NewMortgage(object):
def Init(self):
  self.txtLoanAmount = LogicalTreeHelper.FindLogicalNode(leanGlobal.oGlobal.Sender, 'txtLoanAmount')
  self.txtLenderChaps = LogicalTreeHelper.FindLogicalNode(leanGlobal.oGlobal.Sender, 'txtLenderChaps')
  self.txtCashBack = LogicalTreeHelper.FindLogicalNode(leanGlobal.oGlobal.Sender, 'txtCashBack')
  self.txtRetention = LogicalTreeHelper.FindLogicalNode(leanGlobal.oGlobal.Sender, 'txtRetention')
  self.txtNETAdvance = LogicalTreeHelper.FindLogicalNode(leanGlobal.oGlobal.Sender, 'txtNETAdvance')
  self.AddEvents()

def AddEvents(self):
    self.RemoveEvents()
    self.txtLoanAmount.TextChanged += self.evtMortgage_RecalcNet
    self.txtLenderChaps.TextChanged += self.evtMortgage_RecalcNet
    self.txtCashBack.TextChanged += self.evtMortgage_RecalcNet
    self.txtRetention.TextChanged += self.evtMortgage_RecalcNet

def RemoveEvents(self):
    self.txtLoanAmount.TextChanged -= self.evtMortgage_RecalcNet
    self.txtLenderChaps.TextChanged -= self.evtMortgage_RecalcNet
    self.txtCashBack.TextChanged -= self.evtMortgage_RecalcNet
    self.txtRetention.TextChanged -= self.evtMortgage_RecalcNet

def evtMortgage_RecalcNet(self,sender, e):
    self.RemoveEvents()
    if (self.txtLoanAmount.Text==""):
        self.txtLoanAmount.Text="0"
    if (self.txtLenderChaps.Text==""):
        self.txtLenderChaps.Text="0"
    if (self.txtCashBack.Text==""):
        self.txtCashBack.Text="0"
    if (self.txtRetention.Text==""):
        self.txtRetention.Text="0"
    self.txtNETAdvance.Text=(float(self.txtLoanAmount.Text.replace(',', ''))
            -float(self.txtLenderChaps.Text.replace(',', ''))
            +float(self.txtCashBack.Text.replace(',', ''))
            -float(self.txtRetention.Text.replace(',', ''))).ToString()  
    self.AddEvents()

NewMortgage.txtLoanAmount=object
NewMortgage.txtLenderChaps=object
NewMortgage.txtCashBack=object
NewMortgage.txtRetention=object
NewMortgage.txtNETAdvance=object

oNewMortgage=NewMortgage()

我首先创建Global类:

leanGlobal.oGlobal.Init(_tikitEntity,_tikitMatter.ToString(),_tikitDbAccess,_tikitSender)

然后我调用calc模块(leanNewMortgage.py),一切正常

 leanNewMortgage.oNewMortgage.Init()

注意我还添加了事件,并在触发时临时删除它们。 这是因为我想要一个中心事件,而不是为每个文本框创建一个事件。当事件修改文本框时,需要暂时禁用事件,否则将发生无限循环!

关于这个问题,我的主要教训是如何创建一个整洁的程序结构。

虽然我是IronPython的新手,但我很满意这个结构,但我很乐意听到任何评论。