【问题标题】:paint program undo redo function using temp files?使用临时文件绘制程序撤消重做功能?
【发布时间】:2014-12-31 22:50:54
【问题描述】:

我正在创建一个带有撤销重做功能的绘画程序。我通过拍摄画布的快照然后将它们保存为 jpeg 文件来实现它。然后,我将文件的名称添加到稍后获取的列表中。如何使这些 jpeg 文件成为临时文件?我应该使用什么模块?我正在使用 python 3.2.5。谢谢你的帮助!

这是我的一些代码

    if mb[0]==0 and count==1:                       #if a drawing was made 
            count1+=1                                   #my filename assigner
            fileName=str(count1)+".jpg"                 #creates fileName
            image.save(screen.subsurface(touch),fileName)#saves image
            nowlist.append(fileName)                    #adds to nowlist
            count=0                                     #resets counter
            if len(nowlist)>1:                          #removes old data
                undolist.append(nowlist[0])             #adds it to undolist
                del(nowlist[0])                         #dels it
            print("nowlist :",nowlist)                  #prints lists
            print("undolist :",undolist)
    if undoRect.collidepoint((mx,my)) and mb[0]==1: #if mouse is over and clicks undo
        if len(undolist)>0:                         #if there are at least 1 item 
            fileName=undolist[-1]                   #last item of undolist
            canvas1=image.load(fileName)            #fetch file image
            canvas2=screen.blit(canvas1,[150,150])  #blit it onto the canvas
            nowlist.append(fileName)                #adds it to the canvas list
            if len(nowlist)>1:                      #ensures there is only 1 item on the canvas
                redolist.append(nowlist[0])
                del(nowlist[0])                              
            del(undolist[-1])                       
            print("nowlist :",nowlist)              
            print("undolist :",undolist)            #prints undolist
            print("redolist :",redolist)            #prints redolist
    if redoRect.collidepoint((mx,my)) and mb[0]==1: #if mouse is over and clicks redo
        if len(redolist)>0:                         #if there is at least 1 item 
            fileName=redolist[-1]                   #take last term
            canvas1=image.load(fileName)            #fetch file of that name
            canvas2=screen.blit(canvas1,[150,150])  #blit it onto the canvas
            nowlist.append(fileName)                #append it to the nowlist,
            if len(nowlist)>1:                      #removes what was on the canvas
                undolist.append(nowlist[0])         #adds it to the undo list
                del(nowlist[0])                     #del from nowlist
            del(redolist[-1])                       
            print("nowlist :",nowlist)              #prints the lists    
            print("undolist :",undolist)
            print("redolist :",redolist)

【问题讨论】:

  • 为 undo-redo 保留 jpg 的集合确实很昂贵。如果您有时间,我建议您阅读 Bertrand Meyer 的 "Object Oriented Software Construction" 第 21 章(继承案例研究:交互式系统中的“撤消”)关于实现撤消重做。通过只保留差异而不是每次更改的整个文件,它将为您提供一种简单有效的实施方式。

标签: file python-3.x temp undo-redo


【解决方案1】:

查看 tempfile 模块以制作临时文件和目录。

至于撤消,首先考虑在文本编辑器中撤消/重做。对于每个文本更改,都会有一个反向更改撤消更改。这些是您保存的内容,而不是整个文件的快照。对于大块更改,撤消可能比典型的“差异”更紧凑。对于块替换(例如在用粘贴替换选择时执行),需要保存块开始和旧文本,但只保存新文本的长度。执行此类撤消时,可以通过从要恢复的文本缓冲区中检索新文本来将其转换为重做。必须决定是允许“无限”撤消(直到内存耗尽)还是设置固定限制。

对于二维图形,set((x,y), get(x,y)) 撤消 set((x,y), color)。除此之外,由于像素数量比字符多得多,而且它们的平面而不是线性组织,事情往往会更加复杂。但上述原则仍然适用。

【讨论】:

    猜你喜欢
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    相关资源
    最近更新 更多