Changesbch 发表于 2020-3-27 15:51:30

AttributeError: 'int' object has no attribute 'rect'

pos_start = event.pos
......
.....

select.rect.left,select.rect.top = pos_start
AttributeError: 'int' object has no attribute 'rect'

请问下这个报错如何解决,谢谢

wuqramy 发表于 2020-3-27 15:55:06

发出所有代码

Changesbch 发表于 2020-3-27 15:59:28

wuqramy 发表于 2020-3-27 15:55
发出所有代码

import pygame
from pygame.locals import *


pygame.init()

screen = pygame.display.set_mode()
keep_going = True
timer = pygame.time.Clock()
bg_color = (255,255,255)
pygame.display.set_caption("Pygame")

image = pygame.image.load("z.jpg")
chop_image = pygame.transform.chop(image,(207,200,50,50))

position = chop_image.get_rect()
width = screen.get_rect().width
height = screen.get_rect().height
position.center = width // 2, height // 2

select = 0
select_rect = pygame.Rect(0,0,0,0)
drag = 0


while keep_going:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            pygame.quit()
      elif event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                #第一次点击,选择范围
                if select == 0 and drag == 0:
                  pos_start = event.pos
                  select = 1
                # 第二次点击,拖拽图像
                elif select == 2 and drag == 0:
                  capture = screen.subsurface(select.rect).copy()
                  cap_rect = capture.get_rect()
                  drag = 1
                # 第三次点击,初始化
                elif select == 2 and drag == 2:
                  select = 0
                  drag = 0
      elif event.type == MOUSEBUTTONUP:
            if event.buttton == 1:
                # 第一次释放,结束选择
                if select == 1 and drag == 0:
                  pos_stop = event.pos
                  select = 2
                # 第二次释放,结束拖拽
                elif select == 2 and drag == 1:
                  drag = 2

    screen.fill(bg_color)
    screen.blit(image,position)

    # 实时绘制选择框
    if select:
      mouse_pos = pygame.mouse.get_pos()
      if select == 1:
            pos_stop = mouse_pos
      select.rect.left,select.rect.top = pos_start
      select.rect.width,select.rect.height = pos_stop - pos_start,pos_stop - pos_start
      pygame.draw.rect(screen,(0,0,0),select.rect,1)

      
    # 拖拽裁剪的图像
    if drag:
      if drag == 1:
            cap_rect.center = mouse_pos
      screen.blit(capture,cap_rect)

    pygame.display.flip()

pygame.quit()

wuqramy 发表于 2020-3-27 16:04:30

Changesbch 发表于 2020-3-27 15:59
import pygame
from pygame.locals import *



是select_rect,不是select.rect

sunrise085 发表于 2020-3-27 16:05:09

Changesbch 发表于 2020-3-27 15:59
import pygame
from pygame.locals import *



这个错误很简单啊,
你拼写错了
应该是select _rect.left    select _rect.top
你把'.'写成'_'了

Changesbch 发表于 2020-3-27 16:11:39

sunrise085 发表于 2020-3-27 16:05
这个错误很简单啊,
你拼写错了
应该是select _rect.left    select _rect.top


是的,我刚刚查到了,好几处都打错了,谢谢你啊

Changesbch 发表于 2020-3-27 16:12:24

wuqramy 发表于 2020-3-27 16:04
是select_rect,不是select.rect

查到了,好几处拼写错误,谢谢你

sunrise085 发表于 2020-3-27 16:14:54

Changesbch 发表于 2020-3-27 16:11
是的,我刚刚查到了,好几处都打错了,谢谢你啊

那就给个最佳吧。
这个月在冲量

wuqramy 发表于 2020-3-27 16:16:33

本帖最后由 wuqramy 于 2020-3-27 16:18 编辑

Changesbch 发表于 2020-3-27 16:12
查到了,好几处拼写错误,谢谢你

那就标记已解决,再设个最佳答案吧
页: [1]
查看完整版本: AttributeError: 'int' object has no attribute 'rect'