免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3343 | 回复: 1
打印 上一主题 下一主题

wxPython 仿照wxWidgets的sample例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-21 18:50 |只看该作者 |倒序浏览
#!/usr/bin/python
#coding:gbk
import wx
import wx.animate
ID_PLAY = 1
ID_SET_NULL_ANIMATION = 2
ID_SET_INACTIVE_BITMAP = 3
ID_SET_NO_AUTO_RESIZE = 4
ID_SET_BGCOLOR = 5

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title, pos, size, style):
        wx.Frame.__init__(self, parent, id, title, pos, size, style | wx.NO_FULL_REPAINT_ON_RESIZE)
        self.app = wx.Panel(self, pos=(300, 300))


        wx.EVT_MENU(self, wx.ID_STOP, self.OnStop)
        wx.EVT_MENU(self, ID_PLAY, self.OnPlay)
        wx.EVT_MENU(self, ID_SET_NULL_ANIMATION, self.OnSetNullAnimation)
        wx.EVT_MENU(self, ID_SET_NO_AUTO_RESIZE, self.OnSetNoAutoResize)
        wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
        wx.EVT_MENU(self, wx.ID_EXIT, self.OnQuit)
        wx.EVT_MENU(self, ID_SET_BGCOLOR, self.OnSetBgColor)
        wx.EVT_MENU(self, wx.ID_OPEN, self.OnOpen)
        #wx.EVT_SIZE(self, self.OnSize)
        wx.EVT_UPDATE_UI(self, wx.ID_ANY, self.OnUpdateUI)

        sz = wx.BoxSizer(wx.VERTICAL);
        sz.Add(wx.StaticText(self.app, -1, "wxAnimationCtrl:"));
        self.m_animationCtrl = wx.animate.AnimationCtrl(self.app, wx.ID_ANY)

        if (self.m_animationCtrl.LoadFile("throbber.gif")):
            self.m_animationCtrl.Play()

        sz.Add(self.m_animationCtrl);
        self.app.SetSizer(sz);
        self.Layout()        

    def OnPlay(self, event):
        if (not self.m_animationCtrl.Play()):
            wx.LogError("Invalid animation")

    def OnSetNullAnimation(self, event):
            self.m_animationCtrl.SetAnimation(wx.animate.NullAnimation);

    def OnSetNoAutoResize(self, event):
        #// recreate the control with the new flag if necessary
        rs = (event.IsChecked() and [wx.animate.AC_NO_AUTORESIZE] or [0] )[0]
        style = wx.animate.AC_DEFAULT_STYLE | rs
   
        if (style != self.m_animationCtrl.GetWindowStyle()):
            #// save status of the control before destroying it
            curr = self.m_animationCtrl.GetAnimation();
            inactive = self.m_animationCtrl.GetInactiveBitmap();
            bg = self.m_animationCtrl.GetBackgroundColour();

            #// destroy & rebuild
            old = self.m_animationCtrl;
            self.m_animationCtrl = wx.animate.AnimationCtrl(self, wx.ID_ANY, curr, wx.DefaultPosition, wx.DefaultSize, style);
            self.app.GetSizer().Replace(old, self.m_animationCtrl);
            del(old)
            #// load old status in new control
            self.m_animationCtrl.SetInactiveBitmap(inactive);
            self.m_animationCtrl.SetBackgroundColour(bg);
            self.app.GetSizer().Layout();
    def OnSetBgColor(self, event):
        clr = wx.GetColourFromUser(self, self.m_animationCtrl.GetBackgroundColour(), "Choose the background colour")
        print clr.IsOk()
        if (clr.IsOk()):
            self.m_animationCtrl.SetBackgroundColour(clr)

    def OnStop(self, event):
            self.m_animationCtrl.Stop();

    def OnAbout(self, event):
        msg =  "This is the About dialog of the AccessTest sample.\n Welcome to %s" % wx.VERSION_STRING
        wx.MessageBox(msg, "About AccessTest", wx.OK | wx.ICON_INFORMATION, self)

    def OnQuit(self, event):
        self.Close()

    def OnOpen(self, event):
        dialog = wx.FileDialog(self, "Please choose an animation", wx.EmptyString, wx.EmptyString, "*.gif;*.ani", wx.FD_OPEN);
        filename = ""
        if (dialog.ShowModal() == wx.ID_OK):
            filename = dialog.GetPath()

            #// enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl
            if (self.m_animationCtrl.LoadFile(filename)):
                self.m_animationCtrl.Play()
            else:
                wx.MessageBox("Sorry, this animation is not a valid format for wxAnimation.")

    def OnSize(self, event):
        pass
    def OnUpdateUI(self, event):
        #fp = open("event.log", "w")
        #for x in dir(self.GetMenuBar()):
        #    print x
        #    fp.write(x +"\n")
        #fp.close()
        #exit()
        #C++ FindItem = python FindItemById
        self.GetMenuBar().FindItemById(wx.ID_STOP).Enable(self.m_animationCtrl.IsPlaying())
        self.GetMenuBar().FindItemById(ID_PLAY).Enable(not self.m_animationCtrl.IsPlaying())
        self.GetMenuBar().FindItemById(ID_SET_NO_AUTO_RESIZE).Enable(not self.m_animationCtrl.IsPlaying())
#######################################################

论坛徽章:
0
2 [报告]
发表于 2009-05-21 18:51 |只看该作者

回复 #1 redskywy 的帖子

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, wx.ID_ANY, "Animation Demo", wx.DefaultPosition, wx.Size(480, 320), wx.DEFAULT_FRAME_STYLE)
        frame.SetIcon(wx.Icon("62.ico", wx.BITMAP_TYPE_ICO))

        file_menu = wx.Menu()
        file_menu.Append(wx.ID_OPEN, "&Open Animation", "Loads an animation")
        file_menu.Append(wx.ID_EXIT)

        play_menu = wx.Menu()
        play_menu.Append(ID_PLAY, "Play", "Play the animation")
        play_menu.Append(wx.ID_STOP, "Stop", "Stop the animation")
        play_menu.Append(ID_SET_NULL_ANIMATION, "Set null animation", "Sets the empty animation in the control")
        play_menu.AppendCheckItem(ID_SET_INACTIVE_BITMAP, "Set inactive bitmap", "Sets an inactive bitmap for the control")
        play_menu.AppendCheckItem(ID_SET_NO_AUTO_RESIZE, "Set no autoresize", "Tells the control not to resize automatically");
        play_menu.Append(ID_SET_BGCOLOR, "Set background colour...", "Sets the background colour of the control");

        help_menu = wx.Menu()
        help_menu.Append(wx.ID_ABOUT)

        menu_bar = wx.MenuBar()
        menu_bar.Append(file_menu, "&File")
        menu_bar.Append(play_menu, "&Animation")
        menu_bar.Append(help_menu, "&Help")

        #// Associate the menu bar with the frame
        frame.SetMenuBar(menu_bar)


        frame.CreateStatusBar()

        frame.Show(True)
        self.SetTopWindow(frame)

        return True
#######################################

if __name__ == "__main__":
    app = MyApp()
    app.MainLoop()
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP