【发布时间】:2019-12-27 19:44:04
【问题描述】:
我只是python的初学者。我的代码一天前可以工作,但是当我重新打开它时,它突然停止工作。我正在创建一个绘画程序并制作了一个矩形形状,但是错误总是出现在一行代码中,而且它从来没有这样做过。
我没有触摸我的代码,现在程序运行了,但是当我尝试绘制矩形本身时出现错误。我试图重新定义“screenCap”变量,但它没有帮助
ERROR: Traceback (most recent call last): File "C:\Users\Mazen\Desktop\paint project 1\paintproject.py", line 498, in <module> screen.blit(screenCap,(400,70)) TypeError: argument 1 must be pygame.Surface, not builtin_function_or_method >>>
from pygame import *
from random import *
from math import *
from tkinter import *
root=Tk()
root.withdraw() #hides the extra window
width,height=1280,695
screen=display.set_mode((width,height))
RED=(255,0,0)
GREY=(127,127,127)
BLACK=(0,0,0)
BLUE=(0,0,255)
GREEN=(0,255,0)
YELLOW=(255,255,0)
WHITE=(255,255,255)
tool="NO TOOL"
omx,omy,mx,my=0,0,0,0
sx=0 #Starting x
sy=0
col=(0,0,0)
Shaperad=1 #thickness for all unfilled shapes
Prad=1 #thickness for the pencil
rad=3 #thickness for the brush, eraser, highlighter and spray paint
hlighter=Surface((20,20),SRCALPHA)#for changing the transparency
r,g,b,a=(255,165,0,255) #(r-red,g-green,b-blue,a-alpha)these variables are used to chnage the colours of the highlighter including transparency (ALPHA)
#background
backgroundPic=image.load("images/background.jpg")
screen.blit(backgroundPic,(0,0))
#the canvas
canvas=Rect(400,70,850,500)
draw.rect(screen,WHITE,canvas)
#tools
pencil=Rect(30,180,60,60)
eraser=Rect(110,180,60,60)
brush=Rect(190,180,60,60)
spraypaint=Rect(30,260,60,60)
highlighter=Rect(110,260,60,60)
oval=Rect(30,340,60,60)
rectangle=Rect(110,340,60,60)
line=Rect(190,340,60,60)
filledoval=Rect(30,420,60,60)
filledrect=Rect(110,420,60,60)
polygon=Rect(190,420,60,60)
#displaying tools
draw.rect(screen,BLUE,rectangle)
draw.rect(screen,BLUE,line)
draw.rect(screen,BLUE,oval)
draw.rect(screen,BLUE,filledoval)
draw.rect(screen,BLUE,filledrect)
draw.rect(screen,BLUE,polygon)
#tool photos
rectanglePic=image.load("images/rectangle.png")
screen.blit(rectanglePic,(115,346))
linePic=image.load("images/line.png")
screen.blit(linePic,(190,345))
ovalPic=image.load("images/oval.png")
screen.blit(ovalPic,(33,340))
screenCap=screen.subsurface(canvas).copy()#taking a PARTIAL screen shot
canvas2=screen.subsurface(canvas).copy()#clear copy of the canvas
Rlist=[canvas2]
Ulist=[canvas2]
running=True
while running:
for evt in event.get():
if evt.type==QUIT:
running=False
screenCap=screen.subsurface(canvas).copy()
#controling thickness
if evt.type==KEYDOWN: #some key was pressed
if evt.key==K_UP and tool=="pencil":
Prad+=1 #the thickness of the pencil increases by 1
if Prad>3: #Checks to see if the thicknes goes over 3 and if it does, then it becomes 3
Prad=3
if evt.key==K_UP:
Shaperad+=1
if Shaperad>30:
Shaperad=30
if evt.key==K_UP:
rad+=1
if rad>40:
rad=40
if evt.key==K_DOWN:
Prad-=1
if Prad==0:
Prad=1
Shaperad-=1
if Shaperad==0:
Shaperad=1
rad-=1
if rad==0:
rad=1
#mouse visibility
if evt.type==MOUSEBUTTONDOWN:
mouse.set_visible(False)
if evt.type==MOUSEBUTTONUP:
mouse.set_visible(True)
if evt.type==MOUSEBUTTONDOWN:
sx,sy=evt.pos
screenCap=screen.subsurface(canvas).copy()#taking a screenshot
if evt.type==MOUSEBUTTONUP:
screen.set_clip(canvas)
#undo
if evt.type==MOUSEBUTTONDOWN:
if mb[0]==1:
click=True
screenCap=screen.copy #taking a screenshot
sx,sy=mouse.get_pos()
if evt.type==MOUSEBUTTONUP:
if canvas.collidepoint(mx,my): #If the canvas rect/s collidepoint is the mouse (mouse touches canvas)
duplicate=screen.subsurface(canvas).copy() #makes a duplicate of the surface of the canvas
Ulist.append(duplicate) #appends the dupolicated surface of the canvas
mx,my=mouse.get_pos()
mb=mouse.get_pressed()
#hover colour change
if pencil.collidepoint(mx,my):
draw.rect(screen,GREEN,pencil,3)
else:
draw.rect(screen,BLUE,pencil,3)
if eraser.collidepoint(mx,my):
draw.rect(screen,GREEN,eraser,3)
else:
draw.rect(screen,BLUE,eraser,3)
if rectangle.collidepoint(mx,my):
draw.rect(screen,GREEN,rectangle,3)
else:
draw.rect(screen,BLUE,rectangle,3)
if line.collidepoint(mx,my):
draw.rect(screen,GREEN,line,3)
else:
draw.rect(screen,BLUE,line,3)
if oval.collidepoint(mx,my):
draw.rect(screen,GREEN,oval,3)
else:
draw.rect(screen,BLUE,oval,3)
if brush.collidepoint(mx,my):
draw.rect(screen,GREEN,brush,3)
else:
draw.rect(screen,BLUE,brush,3)
if spraypaint.collidepoint(mx,my):
draw.rect(screen,GREEN,spraypaint,3)
else:
draw.rect(screen,BLUE,spraypaint,3)
if highlighter.collidepoint(mx,my):
draw.rect(screen,GREEN,highlighter,3)
else:
draw.rect(screen,BLUE,highlighter,3)
if filledoval.collidepoint(mx,my):
draw.rect(screen,GREEN,filledoval,3)
else:
draw.rect(screen,BLUE,filledoval,3)
if filledrect.collidepoint(mx,my):
draw.rect(screen,GREEN,filledrect,3)
else:
draw.rect(screen,BLUE,filledrect,3)
if polygon.collidepoint(mx,my):
draw.rect(screen,GREEN,polygon,3)
else:
draw.rect(screen,BLUE,polygon,3)
if gingerbread.collidepoint(mx,my):
draw.rect(screen,BLUE,gingerbread,3)
else:
draw.rect(screen,GREEN,gingerbread,3)
if gift.collidepoint(mx,my):
draw.rect(screen,BLUE,gift,3)
else:
draw.rect(screen,GREEN,gift,3)
if santa.collidepoint(mx,my):
draw.rect(screen,BLUE,santa,3)
else:
draw.rect(screen,GREEN,santa,3)
if sleigh.collidepoint(mx,my):
draw.rect(screen,BLUE,sleigh,3)
else:
draw.rect(screen,GREEN,sleigh,3)
if stalking.collidepoint(mx,my):
draw.rect(screen,BLUE,stalking,3)
else:
draw.rect(screen,GREEN,stalking,3)
if elf.collidepoint(mx,my):
draw.rect(screen,BLUE,elf,3)
else:
draw.rect(screen,GREEN,elf,3)
if tree.collidepoint(mx,my):
draw.rect(screen,BLUE,tree,3)
else:
draw.rect(screen,GREEN,tree,3)
if mistletoe.collidepoint(mx,my):
draw.rect(screen,BLUE,mistletoe,3)
else:
draw.rect(screen,GREEN,mistletoe,3)
#tool selection
if mb[0]==1 and rectangle.collidepoint(mx,my):
tool="rectangle"
draw.rect(screen,RED,rectangle,3)
if mb[0]==1 and line.collidepoint(mx,my):
tool="line"
draw.rect(screen,RED,line,3)
if mb[0]==1 and oval.collidepoint(mx,my):
tool="oval"
draw.rect(screen,RED,oval,3)
if mb[0]==1 and save.collidepoint(mx,my):
try:
fname=filedialog.asksaveasfilename(defaultextension=".png")
print(fname)
if fname!="": #This checks if a file name has been entered
image.save(screen.subsurface(canvasRect),fname) #Saving the picture
image.save(screen.copy(),fname)
except:
print("Saving error")
if mb[0]==1 and load.collidepoint(mx,my):
try:
fname=filedialog.askopenfilename()
if fname!="":
Pic=image.load(fname)
width=Pic.get_width() #recieves the width of the image/file that is being opened
height=Pic.get_height() #recieves the height of the image/file that is being opened
if width>400: #checks to see if the width of the picture is larger than the width of the whole canvas
file=transform.scale(Pic,(400,height)) #changes the width if it is too large
if height>70: #checks to see if the height of the picture is larger than the height of the whole canvas
file=transform.scale(Pic,(width,70)) #changes the height if it is too large
if height>70 and width>400:
file=transform.scale(Pic,(400,70)) #This transforms the width if its larger
draw.rect(screen,WHITE,canvas) #This draws a white rectangle so that when you blit a picture it covers everything, including your previous drawings
screen.blit(file,(canvas)) #This blits the picture onto the canvas
except:
print("load error")
if mb[0]==1 and redo.collidepoint(mx,my):
if len(Rlist)!=1: #checks to see if the redo list is not equal to one
Ulist.append(Rlist[-1])#brings back the last thing that was in the Ulist
Rlist.pop()#removes the last thing on the Rlist
screen.blit(Ulist[-1],(canvas))
if mb[0]==1 and undo.collidepoint(mx,my):
if len(Ulist)!=1:#checks to make sure that the length of the Ulist is not 1 because if it was not 1 it will undo, but if it is 1 and you click undo, the canvas will erase
Rlist.append(Ulist[-1])
Ulist.pop()
screen.blit(Ulist[-1],(canvas))
#using tools
if mb[0]==1:
if canvas.collidepoint(mx,my):
screen.set_clip(canvas)
if tool=="eraser":
#no spaces for eraser
dx=mx-omx #run
dy=my-omy #rise
dist=int(sqrt(dx**2+dy**2))
for i in range(1,dist):
cx=int(omx+i*dx/dist)
cy=int(omy+i*dy/dist)
draw.circle(screen,WHITE,(cx,cy),rad)
if tool=="rectangle":
screen.blit(screenCap,(400,70))
draw.rect(screen,col,(sx,sy,mx-sx,my-sy),Shaperad)
#these rects are to fix the gap error when increasing the thickness of the rectangle
draw.rect(screen,col,(sx-(Shaperad/2-1),sy-(Shaperad/2-1),Shaperad,Shaperad))
draw.rect(screen,col,(sx-(Shaperad/2-1),my-(Shaperad/2),Shaperad,Shaperad))
draw.rect(screen,col,(mx-(Shaperad/2),sy-(Shaperad/2-1),Shaperad,Shaperad))
draw.rect(screen,col,(mx-(Shaperad/2),my-(Shaperad/2),Shaperad,Shaperad))
【问题讨论】:
-
我建议不要手动指出哪一行触发了错误,而是在您的问题中包含完整的回溯,因为它包含导致错误的行以及可能有助于识别问题的其他信息。
-
@TomasFarias 抱歉,这样更好吗?
-
能否请您在单独的代码块中提供整个错误消息,例如:
Traceback (most recent call last): File "typeerror.py", line 1, in <module> print("%d" % ([1,2])) TypeError: %d format: a number is required, not list -
@SergeyNudnov 我在文本中包含了整个错误
-
好的,从显示的代码来看,一切都是正确的。但是代码缩进不一致,这可能会导致您的问题。在 Python 中,正确的缩进是最重要的!清理您的文件并确保所有块都有一致的 4 个空格缩进。然后使用对齐良好的代码更新问题。您是否还提供了
while running:循环下的所有代码行?如果没有,滚动它们并检查带有screenCap变量的任何行。你必须在下面的某个地方重新定义它,在你没有在那里提供的代码中。
标签: python python-3.x button project drawrectangle