Ar3dhel
Posts: 1
Joined: Mon Dec 11, 2017 4:01 pm

Difficulty with buttons not noticing clicks

Mon Dec 11, 2017 4:17 pm

Hi.
I've been writing the code for a program that is eventually going to be an MP3 player. I haven't really got that far - I've just been making it read music files and organise them by folder. It displays each track as a button you can click on to play the song.
And here is the problem :(. The first button works fine. It'll do what it's supposed to do with only one click.
The others - it doesn't matter which file the represent, it's just the ones that are lower down in the list - take multiple clicks to make them work. Pretty much you have to keep clicking on them very quickly before they'll do anything. It doesn't matter whether other files are playing or not.
I haven't been able to solve this problem. I haven't even been able to find out what's causing the problem! I would appreciate any help.

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from os import listdir
from os.path import isfile, join
from sys import exit
import pygame
from pygame.locals import *

WIDTH = 640
HEIGHT = 480

pygame.init()

screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
font = pygame.font.SysFont("arial", 16)
font_height = font.get_linesize()

albums = {}
audiobooks = {}

def setup(directory):
	"""Reads all music and audiobook files and sorts them by directory and type"""
	
	dir_type = ""
	
	for direc in listdir(directory): #Reads all files and directories 
		if not isfile(join(directory, direc)): #Only reads directories
			if "-music" in direc:
				dir_type = "music"
				albums.update({direc: []})
			elif "-book" in direc:
				dir_type = "book"
				audiobooks.update({direc: []})
			for filename in listdir(directory + direc):
				if isfile(join(directory + direc, filename)):
					if dir_type == "music":
						albums[direc].append(filename)
					elif dir_type == "book":
						audiobooks[direc].append(filename)
					
class Album(object):
	"""Handles one specific album of music"""
	
	def __init__(self, key):
		self.title = key.replace("-music", "")
		self.music = []
		music = albums[key]
		y = 60
		for song in music:
			self.music.append(Button(song, 0, y))
			y += 60
		print self.title 
		print self.music
		self.header = pygame.transform.scale(pygame.image.load("./images/header.png"), (640, font_height + 30)).convert_alpha()

	def draw(self, screen):
		title = font.render(self.title, True, (255, 255, 255))
		width = title.get_width()
		screen.fill((155, 155, 0))
		screen.blit(self.header, (0, 0))
		screen.blit(title, (WIDTH/2 - width/2, 20))
		
		for song in self.music:
			song.draw(screen)
			for event in pygame.event.get():
				if song.check_clicked(event):
					print song.name
					pygame.mixer.music.load("./" + self.title + "-music/" + song.name)
					pygame.mixer.music.play(1)
			
class Button(object):
	"""Creates a button that reacts to clicks"""
	
	def __init__(self, text, x, y):
		self.name = text
		self.text = font.render(text, True, (255, 255, 255))
		self.rect = pygame.Rect(x, y, WIDTH, 60)
		
	def draw(self, screen):
		if self.rect.collidepoint(pygame.mouse.get_pos()):
			pygame.draw.rect(screen, (200, 200, 0), self.rect)
		else:
			pygame.draw.rect(screen, (155, 155, 0), self.rect)
		screen.blit(self.text, (self.rect.x, self.rect.y + font_height))
		
	def check_clicked(self, event):
		if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
			print "hit"
			if self.rect.collidepoint(pygame.mouse.get_pos()):
				return True
		return False
		
class Menu(object):
	"""Handles choosing which album or book to play"""
	
	def __init__(self):
		options = []
		for album in albums:
			options.append(str(album))
		for audiobook in audiobooks:
			options.append(str(audiobook))
		y = 60
		for song in music:
			self.music.append(Button(song, 0, y))
			y += 60
		print self.title 
		print self.music
		self.header = pygame.transform.scale(pygame.image.load("./images/header.png"), (640, font_height + 30)).convert_alpha()

	def draw(self, screen):
		title = font.render(self.title, True, (255, 255, 255))
		width = title.get_width()
		screen.fill((155, 155, 0))
		screen.blit(self.header, (0, 0))
		screen.blit(title, (WIDTH/2 - width/2, 20))
		
		for song in self.music:
			song.draw(screen)
			for event in pygame.event.get():
				if song.check_clicked(event):
					print song.name
					pygame.mixer.music.load("./" + self.title + "-music/" + song.name)
					pygame.mixer.music.play(1)
	
			
setup("./")
print albums
print audiobooks

for key in albums:
	a = Album(key)


while True:
	a.draw(screen)
	
	pygame.display.update()

Return to “Python”

Who is online

Users browsing this forum: Baidu [Spider], buja, Galbi3000, przemof and 12 guests