您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

在IronPython 2.7中,wpf.LoadComponent方法将连接名称与XAML UI元素相同的所有属性。如果使用IronPython 2.6,则需要使用WombatPM建议的代码。因此,对于IronPython 2.7,如果您使用以下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"  />
        <Text@R_676_2419@ x:Name="text@R_676_2419@" Height="182" HorizontalAlignment="Left" Margin="24,21,0,0" VerticalAlignment="Top" Width="237" />
    </Grid>
</Window>

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

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'IronPyWpf.xaml')
        self._button.Content = 'My Button'
        self._text@R_676_2419@.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_text@R_676_2419@(self):
        return self._text@R_676_2419@

    def set_text@R_676_2419@(self, value):
        self._text@R_676_2419@ = value

    text@R_676_2419@ = property(get_text@R_676_2419@, set_text@R_676_2419@)

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

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

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

python 2022/1/1 18:34:08 有219人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶