[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Tutorial... GUI



Hi!

    I read a little more about the structure and such. It is slowly getting better to understand. What you have written below is what I need to fully understand this. I did read on the groups thing but trying to understand how to look at the list and grab from the list is what seems to be absent. What you have done below sheds more light onto it.

    I did get the stuff working when I ran the sample program of the sound mixer. I now understand what it wants and what to do. I changed it to add fading, another sound file and watched/listened to it fade. The the busy check did not work, stayed in an infinite loop. Interesting result of the fade feature. But it allowed me to experiment with what it seems to need along with other checks. I added a volume check to exit that loop since the busy check did not work.
I inserted that program below.



    so, having samples of simple programs doing things like adding a button, a radial button and its corresponding data, and others like the one you did below which I think is referring to groups is what I would need to have to learn by experimenting.

        Bruce

-----Original------
I'm not sure what you mean about turning things on. Are you able to get to
a Python programming environment (such as IDLE) at all, and run a "hello
world" program? Or is it that you can't get Pygame running?

Generally what you'd do to start using Pygame/SDL (Pygame is a sort of
wrapper around SDL) is to make sure you have Pygame installed, then at a
Python console or in a program type:
import python
python.init()
screen = pygame.display.set_mode((640,480)) ## Or another size

That should give you a window to start drawing in.

I don't know enough about the GUI needs of a blind user to advise you very
well; what kind of interface do you have in mind? Are you trying to draw a
graphical interface suitable for sighted users, or something very
different?

On the assumption that you're looking to make a simple GUI with ordinary
graphics, a straightforward way of doing that is:
-Create a Widget class that has a location (a Rect object) and a function
for drawing itself (say, a blue rectangle with a white border; you can do
this with pygame.draw.rect).
-Create some instances of this class, giving them locations and keeping a
list of them.
-In your program's main loop, when checking the various Pygame events, say
the following, probably only for MOUSEBUTTONDOWN events:
handled = False
for widget in my_widgets:
  handled = widget.HandleEvent(event) ## or just event.pos
  if handled:
    break
if not handled: ## Respond in some other way.
-Have the HandleEvent function compare the event.pos (the location of the
mouse click) to its own location, using the Pygame "collidepoint"
function. If the position is within the widget's borders, the button was
clicked on.

That's a quick and dirty way of doing a UI, or buttons anyway. In other
words you draw _something_ on the screen, then check whether any mouse
click was within its borders. I did a more elaborate GUI of my own, some
version of which you can find as the messy "Driftwood" code at
<http://kschnee.xepher.net/code/070530ShiningSeaSource.zip>.


#!/usr/bin/env python

"""extremely simple demonstration playing a soundfile
and waiting for it to finish. you'll need the pygame.mixer
module for this to work. Note how in this simple example we
don't even bother loading all of the pygame package. Just
pick the mixer for sound and time for the delay function."""

# Fetch the system path, mixer, and time functions.
import os.path
import pygame.mixer, pygame.time
mixer = pygame.mixer
time = pygame.time

#choose a desired audio format
#(freq, size, stereo, buffersize)
# 1=mono, 2=stereo and 1024 buf size, 2050 for 16 bit
mixer.init(21024) #raises exception on fail

#load the sound from present operating system path to data folder.
file = os.path.join('data', 'secosmic_lo.wav')
file2 = os.path.join('data', 'Trek_Theme.wav')
sound = mixer.Sound(file)
sound2 = mixer.Sound(file2)

#start playing
print 'Playing Sounds...'
channel = sound.play(5) #(loops+1, max ms-time)
channel2 = sound2.play() #(loops+1, max ms-time)
channel.fadeout(10000) # milliseconds to fade out.
# NOTE: fade out above will prevent busy from working, so add volume to while.

#poll until finished
while channel.get_volume() > 0.3 and channel.get_busy(): #still playing
    print 'Volume-', channel.get_volume()
    time.wait(1000)
    if channel.get_busy(): #still playing?
       print '  ...still going...'
print '...Finished'