使用IronPython和Visual Studio 2010进行GUI开发

pin*_*uct 14 python user-interface ironpython visual-studio-2010

我正在使用Python教授编程和GUI开发的入门课程,并且发现对于刚接触编程的学生来说,最不具有压倒性的解决方案是使用Visual Studio进行GUI开发.

虽然使用C#和VB的GUI开发经验令人愉快,但我无法找到一种方法来使用IronPython.我安装了包含Visual Studio工具的IronPython 2.7.1,并创建了一个WPF IronPython项目.

我可以像VB和C#一样使用WPF表单设计器,但是,我找不到可以访问GUI元素的便捷方式(即,可以理解为学生).例如,使用VB,您可以根据名称引用元素,然后可以修改其中的属性.我可以用IronPython(我不打算向学生展示)做的最好的事情如下:

import wpf

from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'WpfApplication3.xaml')

    def Button_Click(self, sender, e):
        #This is the only way I could find in which I can 
        #access an element and modify its properties
        self.Content.Children[1].Text += 'Hello World\n'


if __name__ == '__main__':
    Application().Run(MyWindow())
Run Code Online (Sandbox Code Playgroud)

我注意到GUI元素没有得到名称,每当我尝试手动修改XAML以命名元素时,Visual Studio就会崩溃.以下是带有按钮和文本区域的简单框架的生成XAML:

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="WpfApplication3" Height="300" Width="300"> 
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="103,226,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" />
        <TextBox Height="182" HorizontalAlignment="Left" Margin="24,21,0,0" VerticalAlignment="Top" Width="237" />
    </Grid>
</Window> 
Run Code Online (Sandbox Code Playgroud)

任何帮助学生更容易的帮助将不胜感激.我也对Python GUI开发的其他建议持开放态度,这些建议提供类似于Visual Studio的体验.

Mat*_*ard 6

在IronPython 2.7中,wpf.LoadComponent方法将连接任何与XAML UI元素同名的属性.如果您使用的是IronPython 2.6,那么您需要使用WombatPM建议的代码.因此,如果您使用以下XAML,请使用IronPython 2.7:

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="IronPyWpf" Height="300" Width="300">
    <Grid>
        <Button x:Name="button" Content="Button" Height="23" HorizontalAlignment="Left" Margin="103,226,0,0" VerticalAlignment="Top" Width="75"  />
        <TextBox x:Name="textbox" Height="182" HorizontalAlignment="Left" Margin="24,21,0,0" VerticalAlignment="Top" Width="237" />
    </Grid>
</Window> 
Run Code Online (Sandbox Code Playgroud)

然后,您可以定义两个名为button和textbox的属性来访问UI元素:

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'IronPyWpf.xaml')
        self._button.Content = 'My Button'
        self._textbox.Text = 'My Text'

    def get_button(self):
        return self._button

    def set_button(self, value):
        self._button = value

    button = property(get_button, set_button)

    def get_textbox(self):
        return self._textbox

    def set_textbox(self, value):
        self._textbox = value

    textbox = property(get_textbox, set_textbox)
Run Code Online (Sandbox Code Playgroud)

实际上,通过删除属性定义,您似乎可以进一步简化代码:

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'IronPyWpf.xaml')
        self.button.Content = 'My Button'
        self.textbox.Text = 'My Text'
Run Code Online (Sandbox Code Playgroud)

不幸的是,当您尝试编辑XAML并为UI元素命名时,Visual Studio似乎崩溃,如您所见,带有空引用异常.