我有一个非常简单的Iron Python / Windows Form应用程序。当应用程序启动时,它会在数组中添加一系列标签(这是从CSV文件开始)
#Create Label for each Site
for s in sites:
if lasty == 682:
lasty = 0
lastx = lastx + 125
sitename = str(s) #Convert Name into a String
elementname = sitename.replace(" ","") + "Label"
element = Label()
element.Name = elementname
element.Text = str(sitename)
element.Parent = self
element.Location = Point(lastx,lasty)
labels.append(element) #add each element to the list
lasty = lasty + 22
pass

这很好用。然后我有一个运行另一个循环的定义,根据结果我希望它改变标签的颜色。让我们说Red为失败,Green为OK,Yellow为警告。
但是我似乎无法通过我给他们的元素名称链接到这些标签
例如,我认为SiteNameLabel.ForeColor = Color.Green
会将该标签变为绿色。但是这会失败并停止程序。
有什么想法吗?最有可能是一个简单的问题,但我无法在网上找到任何相关信息。
这是错误:
'ipyw64.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Program Files (x86)\IronPython 2.7\ipyw64.exe'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.Dynamic\v4.0_1.1.1.21__7f709c5b713576e1\Microsoft.Dynamic.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.Scripting\v4.0_1.1.1.21__7f709c5b713576e1\Microsoft.Scripting.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\IronPython\v4.0_2.7.6.3__7f709c5b713576e1\IronPython.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\IronPython.Modules\v4.0_2.7.6.3__7f709c5b713576e1\IronPython.Modules.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Program Files (x86)\IronPython 2.7\DLLs\IronPython.SQLite.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Program Files (x86)\IronPython 2.7\DLLs\IronPython.Wpf.dll'. Cannot find or open the PDB file.
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in IronPython.Modules.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'Anonymously Hosted DynamicMethods Assembly'.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'Snippets.scripting'.
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in IronPython.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in IronPython.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in IronPython.Modules.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in IronPython.Modules.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in IronPython.Modules.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in IronPython.Modules.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in IronPython.Modules.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.Exceptions.ImportException' in Microsoft.Dynamic.dll
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'Snippets.debug.scripting'.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_64\ISymWrapper\v4.0_4.0.0.0__b03f5f7f11d50a3a\ISymWrapper.dll'. Cannot find or open the PDB file.
Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll
Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Cannot find or open the PDB file.
Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll
Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Cannot find or open the PDB file.
'ipyw64.exe' (CLR v4.0.30319: ipyw64.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll'. Cannot find or open the PDB file.
Exception thrown: 'IronPython.Runtime.UnboundNameException' in IronPython.dll
Exception thrown: 'IronPython.Runtime.UnboundNameException' in Snippets.debug.scripting
Exception thrown: 'IronPython.Runtime.UnboundNameException' in Microsoft.Dynamic.dll
Exception thrown: 'IronPython.Runtime.UnboundNameException' in Snippets.debug.scripting
Exception thrown: 'IronPython.Runtime.UnboundNameException' in Snippets.debug.scripting
The program '[9416] ipyw64.exe' has exited with code 1 (0x1).
答案 0 :(得分:0)
您不能使用 SiteNameLabel 作为对象参考。您必须从表单的Controls集合中获取Label的引用。请参阅以下代码:
import clr
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
from System.Drawing import *
from System.Windows.Forms import *
class MyForm(Form):
def __init__(self):
myLabel = Label()
myLabel.Location = Point(10, 10)
myLabel.Text = "EXAMPLE LABEL"
myLabel.Name = "myLabel"
self.Controls.Add(myLabel)
# later you can find label by name this way:
self.Controls["myLabel"].ForeColor = Color.Green
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
form = MyForm()
Application.Run(form)
注意:当您使用WinForms设计器时(在C#项目示例中),它会生成具有控件名称的表单成员,因此您可以在尝试时访问它们。但这仅适用于在设计时添加的控件。