在Python中保存函数问题

2024-10-01 15:49:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的绘制程序创建一个保存特性,我正在使用python2.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)

如果有人能帮我解决这个错误,我将不胜感激。感谢您的帮助。谢谢


Tags: thetextinfromrectimporttrueif
1条回答
网友
1楼 · 发布于 2024-10-01 15:49:16

您可以尝试:

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

顺便说一下,变量txt的设置方式在这里看起来不对。它应该是一个文件名。但是在您的代码示例中,getName是一个函数。你知道吗

相关问题 更多 >

    热门问题