GUI development with IronPython and Visual Studio 2010

前端 未结 2 639
星月不相逢
星月不相逢 2020-12-30 05:08

I\'m teaching an introductory class to programming and GUI development using Python, and have found that the least overwhelming solution for students new to programming is t

2条回答
  •  旧时难觅i
    2020-12-30 05:58

    You need to walk through all of the objects and create the easier/understandable references using a function like.

    #
    # Waddle returns a dictionary of Control types e.g. listbox, Button.
    # Each Entry is a dictionary of Control Instance Names i.e.
    # controls['Button']['NewSite'] returns the button control named NewSite
    # Controls should have Unique names and only those with a Name attrib set
    # will be included.
    #
    def Waddle(c, d):
        s = str(c.__class__)
        if "System.Windows.Controls." in str(c) and hasattr(c,"Name") and c.Name.Length>0:
            ControlType = s[s.find("'")+1:s.rfind("'")]
            if ControlType not in d:
                d[ControlType] = {}
            d[ControlType][c.Name] = c
        if hasattr(c,"Children"):
            for cc in c.Children:
                Waddle(cc, d)
        elif hasattr(c,"Child"):
            Waddle(c.Child, d)
        elif hasattr(c,"Content"):
            Waddle(c.Content, d)
    if __name__ == "__main__":
        xr = XmlReader.Create(StringReader(xaml))
        win = XamlReader.Load(xr)
    
        controls = {}
        Waddle(win, controls)
    
        #Make all Named buttons do something!
        for butt in controls['Button']:
            controls['Button'][butt].Click += sayhello
    
        #Make one button do something.
        controls['Button']['NewSite'].Click += sayhello2
        Application().Run(win)
    

    See http://www.ironpython.info/index.php/XAML_GUI_Events_Example for the above code and a complete example.

提交回复
热议问题