【问题标题】:Save Function Problems in Python在 Python 中保存函数问题
【发布时间】:2014-01-24 02:31:35
【问题描述】:

我目前正在尝试为我使用 python 2.7.5 和 pygame 创建的绘图程序创建一个保存功能。这是我的代码:

from pygame import *
from random import *
from glob import *
from math import *

def getName():
    ans = ""                    # final answer will be built one letter at a time.
    arialFont = font.SysFont("Times New Roman", 16)
    back = screen.copy()        # copy screen so we can replace it when done
    textArea = Rect(5,5,200,25) # make changes here.

    pics = glob("*.bmp")+glob("*.jpg")+glob("*.png")
    n = len(pics)
    choiceArea = Rect(textArea.x,textArea.y+textArea.height,textArea.width,n*textArea.height)
    draw.rect(screen,(220,220,220),choiceArea)        # draw the text window and the text.
    draw.rect(screen,(0,0,0),choiceArea,1)        # draw the text window and the text.
    for i in range(n):
        txtPic = arialFont.render(pics[i], True, (0,111,0))   #
        screen.blit(txtPic,(textArea.x+3,textArea.height*i+choiceArea.y))

typing = True
while typing:
    for e in event.get():
        if e.type == QUIT:
            event.post(e)   # puts QUIT back in event list so main quits
            return ""
        if e.type == KEYDOWN:
            if e.key == K_BACKSPACE:    # remove last letter
                if len(ans)>0:
                    ans = ans[:-1]
            elif e.key == K_KP_ENTER or e.key == K_RETURN : 
                typing = False
            elif e.key < 256:
                ans += e.unicode       # add character to ans

    txtPic = arialFont.render(ans, True, (0,0,0))   #
    draw.rect(screen,(220,255,220),textArea)        # draw the text window and the text.
    draw.rect(screen,(0,0,0),textArea,2)            #
    screen.blit(txtPic,(textArea.x+3,textArea.y+2))

    display.flip()

screen.blit(back,(0,0))
return ans

我认为上面的代码没有问题,因为错误信息出现在下面的代码中:

if saveRect.collidepoint(mx,my):
        txt = getName(True)
        image.save(screen.subsurface(canvas),txt)

这是我收到的错误消息:

Traceback (most recent call last):
File "C:\Users\Wisdom1\Desktop\Comp Science Files\Canvas_(2).py", line 276, in <module>
txt = getName(True)
TypeError: getName() takes no arguments (1 given)

如果有人能帮助我解决这个错误,我将不胜感激。任何帮助表示赞赏。谢谢

【问题讨论】:

  • 如果代码不正确,为什么还要添加所有代码?发生错误的其余代码是什么,image 是如何定义的?

标签: python save pygame paint


【解决方案1】:

你可以试试:

pygame.image.save(screen.subsurface(canvas),txt)

顺便说一句,变量txt 的设置方式在这里看起来并不正确。它应该是一个文件名。但在您的代码示例中,getName 是一个函数。

【讨论】:

  • 在文件开头添加“import pygame”
猜你喜欢
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 2019-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多