免费注册 查看新帖 |

Chinaunix

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

wx处理图片遇到的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-08 02:19 |只看该作者 |倒序浏览
#!/usr/bin/env python   
import wx   
import time
class Frame(wx.Frame):   
    """Frame class that displays an image."""  
    def __init__(self, image, parent=None, id=-1,   
                 pos=wx.DefaultPosition, title='Hello, wxPython!'):   
        """Create a Frame instance and display image."""  
        temp = image.ConvertToBitmap()   
        size = temp.GetWidth(), temp.GetHeight()   
        wx.Frame.__init__(self, parent, id, title, pos, size)   
        panel = wx.Panel(self)   
        self.bmp = wx.StaticBitmap(parent=panel, bitmap=temp)   
        self.SetClientSize(size)   
class App(wx.App):   
    """Application class."""  
    def OnInit(self):  
        image = wx.Image('.\pythondemo\multiline.png', wx.BITMAP_TYPE_PNG)   
        self.frame = Frame(image)  
        self.frame.Show()   
        self.SetTopWindow(self.frame)
        return True  
def main():   
        app = App()
        app.MainLoop()
if __name__ == '__main__':

     while 1:
        time.sleep(1)
        main()


需要实现的功能是在Frame内每秒钟重新加载一次图片:multiline.png
该图片文件已经实现了每秒钟被修改一次,但是执行上述脚本后,frame中的图片却一直没有变化,貌似 time.sleep(1) 根本就没起作用。

请教大家,我该如何修改程序达到我的目的呢?



[ 本帖最后由 xinrao 于 2009-10-8 02:22 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-10-08 08:17 |只看该作者
应该在app类的里面写这个刷新的吧?比如OnTimer或者OnRefresh之类的。
你的main函数里面有个Mainloop,这个函数不会退出,除非你关掉窗口,自然你的while1不会循环。

论坛徽章:
0
3 [报告]
发表于 2009-10-08 17:52 |只看该作者
谢谢楼上兄弟的指点,不过,我没找到ontimer函数的使用方法,麻烦你再指点一下,再次感谢!

论坛徽章:
0
4 [报告]
发表于 2009-10-09 14:48 |只看该作者
#-*- encoding:UTF-8 -*-
import wx
import time
class Frame(wx.Frame):
    def __init__(self, image, parent=None, id=-1,
                 pos=wx.DefaultPosition, title='Hello, wxPython!'):
        temp = image.ConvertToBitmap()
        size = temp.GetWidth(), temp.GetHeight()
        wx.Frame.__init__(self, parent, id, title, pos, size)
        panel = wx.Panel(self)
        self.bmp = wx.StaticBitmap(parent=panel, bitmap=temp)
        self.SetClientSize(size)
class App(wx.App):
    def OnInit(self):
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER,App,self.timer)
        self.timer.Start(1000,oneShot=False)
        image = wx.Image('.\pythondemo\multiline.png', wx.BITMAP_TYPE_PNG)
        self.frame = Frame(image)
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True
def main():
        app = App()
        app.MainLoop()
if __name__ == '__main__':
        main()



已经添加了wx.timer,但现在出现的问题,是每秒钟重新打开一个窗口显示图片,窗口越来越多,能否就只显示在同一个窗口内呢?

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:50:28
5 [报告]
发表于 2009-10-09 15:56 |只看该作者
#! /usr/bin/env python
#coding=utf-8

import wx

class Frame(wx.Frame):
    def update(self,event):
        image = wx.Bitmap('.\pythondemo\multiline.png', wx.BITMAP_TYPE_PNG)   
        self.bmp.SetBitmap(image)
        self.bmp.Show()
        return True
          
    def __init__(self, parent=None, id=-1,
                 pos=wx.DefaultPosition, title='Hello, wxPython!'):
        wx.Frame.__init__(self, parent, id, title, pos)
        panel = wx.Panel(self)
        self.bmp = wx.StaticBitmap(parent=panel)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER,self.update)
        self.timer.Start(1000,oneShot=False)
        
def main():
        app = wx.PySimpleApp()
        frame = Frame()
        app.SetTopWindow(frame)
        frame.Show()
        app.MainLoop()


if __name__ == '__main__':
        main()

论坛徽章:
0
6 [报告]
发表于 2009-10-10 11:03 |只看该作者
按照5楼兄弟的代码,已经可以实现,非常感谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP