我遇到了一個問題,我在視窗中添加了兩個按鈕,但它們會不斷出現并重新出現在視窗上。我不認為它們是任何形式的回圈,但我在這方面可能完全錯了。只要網路攝像頭關閉,按鈕就會保持靜止,但一旦網路攝像頭打開,按鈕就會開始消失并再次出現。我正在尋找一種方法讓按鈕在視窗上保持靜止,同時網路攝像頭處于活動狀態。請注意,我正在使用 cv2 庫在視窗上顯示我的網路攝像頭。
這是我的代碼:
import pygame
import cv2
#import numpy as np
pygame.init()
# setting the width and height of the window
WIDTH, HEIGHT = 1280, 720
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("PANIC")
# background color
color = ("black")
# Webcam
camera = cv2.VideoCapture(0) # 0 is the built in webcam
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1200) # frame width
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1000) # frame height
# function that will display (draw) stuff
def draw_window():
# background color
WINDOW.fill((color))
success, image = camera.read()
if success:
camera_surf = pygame.image.frombuffer(image.tobytes(), image.shape[1::-1], "BGR")
WINDOW.blit(camera_surf, (0,0))
# Buttons
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
# 2. put the collide check for mouse hover here for each button
if b0.rect.collidepoint(pygame.mouse.get_pos()):
b0.colors = "red on green"
elif b1.rect.collidepoint(pygame.mouse.get_pos()):
b1.colors = "red on green"
else:
# this will work for every buttons going back to original color after mouse goes out
not_hover()
if event.type == pygame.MOUSEBUTTONDOWN:
# 3. here the interactions with the click of the mouse... done
if b0.rect.collidepoint(pygame.mouse.get_pos()):
print("ON") # print text of button 1
if b1.rect.collidepoint(pygame.mouse.get_pos()):
print("OFF") # prints text of button 2
buttons.update()
buttons.draw(WINDOW)
# update the display
pygame.display.update()
FPS = 30
def mainWindow():
# keeping the window open
run = True
clock = pygame.time.Clock()
while run:
# capping it at the set frame rate
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
pygame.display.update()
# closing the window
pygame.quit()
def not_hover():
for x in buttons:
x.colors = "red on yellow"
x.update()
# button class
buttons = pygame.sprite.Group()
class Button(pygame.sprite.Sprite):
def __init__(self, WINDOW, position, text, size, colors="white on blue"):
super().__init__()
self.colors = colors
self.fg, self.bg = self.colors.split(" on ")
self.font = pygame.font.SysFont("Arial", size)
self.text_render = self.font.render(text, 1, self.fg)
self.image = self.text_render
self.x, self.y, self.w , self.h = self.text_render.get_rect()
self.x, self.y = position
self.rect = pygame.Rect(self.x, self.y, self.w, self.h)
self.position = position
self.update()
buttons.add(self)
def update(self):
self.fg, self.bg = self.colors.split(" on ")
pygame.draw.line(WINDOW, (150, 150, 150), (self.x, self.y), (self.x self.w , self.y), 5)
pygame.draw.line(WINDOW, (150, 150, 150), (self.x, self.y - 2), (self.x, self.y self.h), 5)
pygame.draw.line(WINDOW, (50, 50, 50), (self.x, self.y self.h), (self.x self.w , self.y self.h), 5)
pygame.draw.line(WINDOW, (50, 50, 50), (self.x self.w , self.y self.h), [self.x self.w , self.y], 5)
pygame.draw.rect(WINDOW, self.bg, (self.x, self.y, self.w , self.h))
b0 = Button(WINDOW, (800, 10), "SWITCH ON", 55, "red on yellow")
b1 = Button(WINDOW, (800, 100), "SWITCH OFF", 55, "red on yellow")
# main #
mainWindow()
uj5u.com熱心網友回復:
這是一個縮進的問題。buttons.update()并且buttons.draw(WINDOW)必須在應用程式回圈而不是事件回圈中呼叫:
def draw_window():
# background color
WINDOW.fill((color))
success, image = camera.read()
if success:
camera_surf = pygame.image.frombuffer(image.tobytes(), image.shape[1::-1], "BGR")
WINDOW.blit(camera_surf, (0,0))
# Buttons
for event in pygame.event.get():
# [...]
# INDENTATION
#<--|
buttons.update()
buttons.draw(WINDOW)
# update the display
pygame.display.update()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/519344.html
上一篇:如何使用帶有THREE.js的lilgui更改2D圓的大小?
下一篇:嘗試從網站上的表格中抓取文本
