【发布时间】:2011-05-02 05:44:06
【问题描述】:
我正在教授一门关于使用 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())
我注意到,当我尝试手动修改 XAML 以命名元素时,GUI 元素没有获得名称并且 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>
如果能帮助学生更轻松地完成这项工作,我们将不胜感激。我也愿意接受其他有关 Python GUI 开发的建议,这些建议提供类似于 Visual Studio 的体验。
【问题讨论】:
-
我发现将 PyQt4 与 Qt Designer 一起使用(对我而言)比使用 Visual Studio 容易得多,因为它轻量且简单。我尝试了几年 Visual Studio,但它非常臃肿。
标签: python visual-studio-2010 user-interface ironpython