使用IronPython和Visual Studio 2010进行GUI开发 [英] GUI development with IronPython and Visual Studio 2010

查看:103
本文介绍了使用IronPython和Visual Studio 2010进行GUI开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 to use Visual Studio for GUI development.

尽管使用C#和VB进行GUI开发的经历令人愉快,但我找不到用IronPython进行同样操作的方法.我安装了包括Visual Studio工具的IronPython 2.7.1,并创建了WPF IronPython项目.

While the GUI development experience with C# and VB is pleasant, I couldn't find a way to do the same with IronPython. I installed IronPython 2.7.1 which includes the Visual Studio tools, and created a WPF IronPython project.

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

I can use the WPF form designer just like VB and C#, however, I couldn't find a convenient way (i.e., comprehensible to the students) in which the GUI elements could be accessed. For example, with VB, you can refer to elements based on their name and then you can modify properties within them. The best I could do with IronPython (which I don't plan to show to the students) is the following:

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())

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

I noticed that the GUI elements do not get a name and Visual Studio crashes whenever I try to manually modify the XAML to name elements. Here is the generated XAML for a simple frame with a button and text area:

<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> 

在此方面使学生更轻松的任何帮助将不胜感激.我也愿意接受其他有关Python GUI开发的建议,这些建议提供的体验类似于Visual Studio.

Any assistance in making this easier on the students would be appreciated. I'm also open to other suggestions for Python GUI development which offer an experience similar to Visual Studio.

推荐答案

在IronPython 2.7中,wpf.LoadComponent方法将连接名称与XAML UI元素相同的所有属性.如果使用IronPython 2.6,则需要使用WombatPM建议的代码.因此,对于IronPython 2.7,如果您使用以下XAML:

In IronPython 2.7 the wpf.LoadComponent method will wire up any properties with the same name as the XAML UI elements. If you are using IronPython 2.6 then you would need to use the code as suggested by WombatPM. So with IronPython 2.7 if you use the following XAML:

<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> 

然后,您可以定义两个属性,分别称为button和textbox,以访问UI元素:

Then you can define two properties called button and textbox to access the UI elements:

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)

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

In fact it seems that you can simplify the code even further by removing the property definitions:

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

不幸的是,正如您已经看到的那样,当您尝试编辑XAML并为UI元素命名时,Visual Studio似乎崩溃了,并且具有空引用异常.

Unfortunately Visual Studio seems to crash, as you have already seen, with a null reference exception when you try to edit the XAML and give the UI elements a name.

这篇关于使用IronPython和Visual Studio 2010进行GUI开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆