【问题标题】:How to figure out what windows forms modules to import in ironpython?如何找出要在 Ironpython 中导入的 Windows 窗体模块?
【发布时间】:2012-09-22 14:57:08
【问题描述】:

我正在尝试从包含in this page 的示例中复制一些代码,并将其修改为在铁蟒using some help from these tutorials 上运行。但是当我走出教程时,我被困住了,我不知道我需要导入哪些模块。

目前我有以下代码

import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")

from System.Windows.Forms import Application, Form, Button, Label, DockStyle, AnchorStyles, Panel, Screen, FlowLayoutPanel

class OKWindow(Form):
   def __init__(self,InfoTXT):
      newlines = 0
      screenSize = Screen.GetWorkingArea(self)
      STRwidth = 200
      STRheight = 30
      FORMheight = 160 
      FORMwidth = 300
      self.Text = 'Information'
      self.Height = FORMheight
      self.Width = FORMwidth


      self.flowPanel = FlowLayoutPanel()
      #self.flowPanel.AutoSize = true
      #self.flowPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
      self.Controls.Add(flowPanel)

      label = Label()
      label.Text = InfoTXT
      label.Top = 30
      label.Left = 50
      label.Height = STRheight
      label.Width = STRwidth

      button = Button()
      button.Text = "OK"
      button.Width = 100
      button.Top = FORMheight - 80
      button.Left = (FORMwidth / 2) - 50
      print button.Anchor
      button.Anchor = AnchorStyles.Bottom

      button.Click += self.buttonPressed

      self.Controls.Add(label)
      self.Controls.Add(button)

   def buttonPressed(self, sender, args):
      Application.Exit()

def information(Message):
   Application.EnableVisualStyles()
   form = OKWindow(Message)
   Application.Run(form)

(注意:代码不准确,因为它目前在 OCTGN 的 Iron python 脚本引擎中运行。我从其他地方调用 information() 函数,并带有一些文本,例如 information('Important Announcement')。)

因此,只要我尝试执行此self.flowPanel = FlowLayoutPanel(),代码就会中止。如果我注释掉 flowPanel 行,windows 窗体就会正常显示。

所以在我看来,我没有正确导入所需的模块。不幸的是,我不知道要加载什么。我尝试加载我认为合适的任何内容,但似乎没有一个适合我。

如何确定要从 System.Windows.Forms 导入什么模块,以便在我的代码中创建 FlowLayoutPanel?通常,如何确定要导入什么才能获得相关功能?

【问题讨论】:

  • 没关系,我想我设法找到了答案:看来要导入的模块名称毕竟是FlowLayoutPanel,但我的代码失败了,因为我在变量前面加上了self.,原因是复制面食>_

标签: python ironpython windowsformsintegration


【解决方案1】:

答案似乎是第三个点“。”之后的任何内容。在 System.Windows.Forms 中

所以要使用System.Windows.Forms.Form,需要导入Form。

【讨论】:

    【解决方案2】:

    所有答案都错了。 导入中缺少 AutoSizeMode,然后代码可以工作

    import clr
    clr.AddReference("System.Drawing")
    clr.AddReference("System.Windows.Forms")
    
    
    
    from System.Windows.Forms import Application, Form, Button, Label, DockStyle, AnchorStyles, Panel, Screen, FlowLayoutPanel,AutoSizeMode
    
    #from System.Windows.Forms import *
    
    
    
    class OKWindow(Form):
        def __init__(self,InfoTXT):
            newlines = 0
            screenSize = Screen.GetWorkingArea(self)
            STRwidth = 200
            STRheight = 30
            FORMheight = 160 
            FORMwidth = 300
            self.Text = 'Information'
            self.Height = FORMheight
            self.Width = FORMwidth
    
    
            flowPanel = FlowLayoutPanel()
            flowPanel.AutoSize = True
            flowPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
    
            print(AutoSizeMode)
            self.Controls.Add(flowPanel)
    
            label = Label()
            label.Text = InfoTXT
            label.Top = 30
            label.Left = 50
            label.Height = STRheight
            label.Width = STRwidth
    
            button = Button()
            button.Text = "OK"
            button.Width = 100
            button.Top = FORMheight - 80
            button.Left = (FORMwidth / 2) - 50
            #print( button.Anchor)
            button.Anchor = AnchorStyles.Bottom
    
            button.Click += self.buttonPressed
    
            self.Controls.Add(label)
            self.Controls.Add(button)
    
        def buttonPressed(self, sender, args):
            Application.Exit()
    
    def information(Message):
        Application.EnableVisualStyles()
        form = OKWindow(Message)
        Application.Run(form)
    
    information('laber')    
    

    【讨论】:

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