已有183人关注
Pygame按钮出不来
发表在Python图书答疑 2021-11-28 悬赏:1 学分 《Python项目开发案例集锦》第15章 智能停车场车牌识别计费系统 327页-329页
是否精华
版块置顶:

btn.py代码:

pygame


Button():
    (screencenterxywidthheightbutton_colortext_colormsgsize):
        .screen = screen
        .width.height = widthheight
        .button_color = button_color
        .text_color = text_color
        .font = pygame.font.SysFont(size)
        .rect = pygame.Rect(.width.height)
        .rect.centerx = centerxy[] - .width / + .rect.centery = centerxy[] - .height / + .deal_msg(msg)

    (msg):
        .msg_img = .font.render(msg.text_color.button_color)
        .msg_img_rect = .msg_img.get_rect()
        .msg_img_rect.center = .rect.center

    ():
        .screen.fill(.button_color.rect)
        .screen.blit(.msg_img.msg_img_rect)

main.py代码:

pygame

pandas
pandas pd
pygame.init()
size = screen = pygame.display.set_mode(size)
FPS = DARKBLUE = ()
BG = DARKBLUE  icon = pygame.image.load()
pygame.display.set_icon(icon)
screen.fill(BG)
clock = pygame.time.Clock()
Running = pygame.display.set_caption()  Running:
    btn_go = btn.Button(screen()BLUEWHITE)
    btn_go.draw_button()
    e pygame.event.get():
        e.type == pygame.QUIT:
            pygame.quit()
            ()
        e.type == pygame.MOUSEBUTTONDOWN:
            <= e.pos[] e.pos[] <= <= e.pos[] e.pos[] <= :
                ()
                :
                    carNumber = ocrutil.getCn()
                e:
                    ()
                    ocrutil.messagebox.showerror((e))
                    img  sucessimg = camara.read()
    :
        cv2.imwrite(img)
    cv2.error:
        os.remove()
        cv2.imwrite(img)  image = pygame.image.load()
    image = pygame.transform.scale(image())
    screen.blit(image())
    pygame.display.flip()
    clock.tick(FPS)

颜色放在colors.py里面,代码:

WHITE = tuple([] * BLACK = tuple([] * GREEN = ()
BLUE = ()
GRAY = tuple([] * RED = ()
YELLOW = tuple([] * + [])

结果显示:

屏幕截图 2021-11-28 141157.png

所以按钮去哪了?Pygame吞掉了?

分享到:
精彩评论 4
王若宇
学分:70 LV3
2021-11-28
沙发

抱歉那个代码全没了

import pygame


# 自定义按钮
class Button(object):
    # msg为要在按钮中显示的文本
    def __init__(self, screen, centerxy, width, height, button_color, text_color, msg, size):
        """初始化按钮的属性"""
        self.screen = screen
        # 按钮宽高
        self.width, self.height = width, height
        # 设置按钮的rect对象颜色为深蓝
        self.button_color = button_color
        # 设置文本的颜色为白色
        self.text_color = text_color
        # 设置文本为默认字体,字号为20
        self.font = pygame.font.SysFont('SimHei', size)
        # 设置按钮大小
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        # 创建按钮的rect对象,并设置按钮中心位置
        self.rect.centerx = centerxy[0] - self.width / 2 + 2
        self.rect.centery = centerxy[1] - self.height / 2 + 2
        # 渲染图像
        self.deal_msg(msg)

    def deal_msg(self, msg):
        """将msg渲染为图像,并将其在按钮上居中"""
        # render将存储在msg的文本转换为图像
        self.msg_img = self.font.render(msg, True, self.text_color, self.button_color)
        # 根据文本图像创建一个rect
        self.msg_img_rect = self.msg_img.get_rect()
        # 将该rect的center属性设置为按钮的center属性
        self.msg_img_rect.center = self.rect.center

    def draw_button(self):
        # 填充颜色
        self.screen.fill(self.button_color, self.rect)
        # 将该图像绘制到屏幕
        self.screen.blit(self.msg_img, self.msg_img_rect)

main

import pygame

import pandas
import pandas as pd

# 主窗口
pygame.init()
size = 1000, 484
screen = pygame.display.set_mode(size)
# 帧率
FPS = 60  # 设置帧率为常量
DARKBLUE = (73, 119, 142)
BG = DARKBLUE  # 设置背景
icon = pygame.image.load("file/ic_launcher.png")
pygame.display.set_icon(icon)
screen.fill(BG)
# 时钟
clock = pygame.time.Clock()
Running = True
# 主循环
pygame.display.set_caption("智能停车场车牌识别收费系统")  # 顺便改了
# 窗口抬头
while Running:
    # 创建识别按钮
    btn_go = btn.Button(screen, (640, 480), 150, 60, BLUE, WHITE, "识别", 25)
    # 绘制按钮
    btn_go.draw_button()
    # 没有显示?
    for e in pygame.event.get():
        # 关闭按钮
        if e.type == pygame.QUIT:
            # 退出
            pygame.quit()
            exit()
        # 这说明这书上的不对,我们看看资源包(其实他直接把这个放到退出里面,肯定更不对)
        elif e.type == pygame.MOUSEBUTTONDOWN:
            if 492 <= e.pos[0] and e.pos[0] <= 642 and 422 <= e.pos[1] and e.pos[1] <= 482:
                print("识别按钮已点击")
                try:
                    # 尝试获取车牌
                    carNumber = ocrutil.getCn()
                except Exception as e:
                    print("识别出错")
                    ocrutil.messagebox.showerror("错误", str(e))
                    continue
            pass

    sucess, img = camara.read()
    try:
        cv2.imwrite("file/test.png", img)
    except cv2.error:
        os.remove("file\\test.png")
        cv2.imwrite("file/test.png", img)  # 还是没有解决
    # 使用pygame加载图片
    image = pygame.image.load("file\\test.png")
    # 设置图片大小为640x480
    image = pygame.transform.scale(image, (640, 480))
    # 显示图片
    screen.blit(image, (2, 2))
    # 更新界面显示
    pygame.display.flip()
    # 控制最大帧率
    clock.tick(FPS)
#colors.py代码:
WHITE = tuple([255] * 3)
BLACK = tuple([0] * 3)
GREEN = (0, 255, 0)
BLUE = (72, 61, 139)
GRAY = tuple([96] * 3)
RED = (220, 20, 60)
YELLOW = tuple([255] * 2 + [0])
无语_mrkj
学分:3155 LV11
2021-11-29
板凳

您运行一下资料包中提供的源码试试。另外,您使用的Pygame和Python都是什么版本?

王若宇
学分:70 LV3
2021-12-03
地板

无语_mrkj 发表于2021-11-29 14:24

您运行一下资料包中提供的源码试试。另外,您使用的Pygame和Python都是什么版本?

知道了,是我把按钮绘制放到最前面了,被图像盖住了。放到后面就好了。贴上图片:

屏幕截图 2021-12-03 193949.png


game99258
学分:2266 LV10
2022-08-07
4L

代码顺序

首页上一页 1 下一页尾页 4 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照