使用IronPython开发跨平台GUI:wx.NET是否具有真正的原生外观?

时间:2011-01-07 16:28:59

标签: .net mono wxpython ironpython wxwidgets

显然,通过IronPython,可以通过为每个平台编写不同的GUI层来创建一流的用户体验(Linux上的GTK#,Windows上的WinForms ......)

我正在认真考虑这样做,尽管我头脑中的小小的计算机科学家正在尖叫。避免这种重复的一个选择是使用wxWidgets工具包,它能够在多个平台上提供真正的原生外观。由于我打算使用IronPython,我想这会涉及使用wx.NET包装器。

我的问题是:是否可以在IronPython中使用wx.NET包装器?更重要的是:在IronPython中使用wx.NET很容易吗?我四处寻找,并没有找到很多人在其他地方使用这种组合的证据。有没有人一起使用这两种技术,或听说过一个项目呢?

谢谢!

1 个答案:

答案 0 :(得分:3)

我花了一些时间玩IronPython和wx.NET库,我发现可以使用IronPython中的wx.NET。我已经创建了一个示例应用程序来演示基本思想(在Linux上使用Mono 2.8.1和IronPython 2.6.1进行测试)。 XRC文件是使用wxFormBuilder创建的。似乎使用IronPython创建一个wx.NET GUI应该很容易;它看起来与等效的C#代码相同。

hello_frame.pyw:

import clr
clr.AddReference("wx.NET.dll")
from wx import *

class MyFrame1(Frame):
    def __init__(self):
        XmlResource.Get().LoadFrame(self, None, "MyFrame1")
        self.EVT_BUTTON( XmlResource.XRCID("m_button1"), EventListener(self.OnMyButtonClicked) )
    def OnMyButtonClicked(self, sender, e):
        MessageDialog.ShowModal( self, "HELLO WORLD!", "", WindowStyles.DIALOG_OK | WindowStyles.ICON_INFORMATION )
class HelloWorldDemo(App):
    def OnInit(self):
        XmlResource.Get().InitAllHandlers()
        XmlResource.Get().Load( "hello_frame.xrc" )
        f = MyFrame1()
        f.Show()
        return True
def main():
    app = HelloWorldDemo()
    app.Run()
if __name__ == '__main__':
    main()

hello_frame.xrc:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<resource xmlns="http://www.wxwindows.org/wxxrc" version="2.3.0.1">
    <object class="wxFrame" name="MyFrame1">
        <style>wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL</style>
        <size>500,300</size>
        <title>Demo</title>
        <centered>1</centered>
        <object class="wxBoxSizer">
            <orient>wxVERTICAL</orient>
            <object class="sizeritem">
                <option>0</option>
                <flag>wxALL</flag>
                <border>5</border>
                <object class="wxStaticText" name="m_staticText1">
                    <label>My Super Program</label>
                    <wrap>-1</wrap>
                </object>
            </object>
            <object class="sizeritem">
                <option>0</option>
                <flag>wxALL</flag>
                <border>5</border>
                <object class="wxTextCtrl" name="m_textCtrl1">
                    <value></value>
                    <maxlength>0</maxlength>
                </object>
            </object>
            <object class="sizeritem">
                <option>0</option>
                <flag>wxALL</flag>
                <border>5</border>
                <object class="wxButton" name="m_button1">
                    <label>Press Me!</label>
                    <default>0</default>
                </object>
            </object>
        </object>
    </object>
</resource>