2014-01-24 56 views
0

我目前正在嘗試爲我使用python 2.7.5和pygame創建的繪圖程序創建一個保存功能。這裏是我的代碼:在Python中保存函數問題

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) 

如果有人能幫我解決這個錯誤,我將不勝感激。任何幫助表示讚賞。由於

+0

爲什麼你添加所有的代碼,如果它不正確的位?什麼是錯誤發生的地方的代碼的其餘部分,如何定義'image'? – 2014-01-24 02:40:09

回答

2

你可以試試:

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

順便說一句,變量txt設置看起來不正確這裏的方式。它應該是一個文件名。但在您的代碼示例中,getName是一個函數。

+0

我有。它給了我錯誤「pygame沒有定義」 – user2950875

+0

在你的文件開頭添加'import pygame' –

+0

謝謝!這固定了我的一個錯誤。 – user2950875