The code below gives a background image with a single button which appears as an image. The button calls a function when clicked. To run the code you will need 3 images. Mine are called splash.png and it is 1024 x 768, and two smaller buttons; a default, and a rollover png of the same size.
The first thing to do is create an egg containing the images. I’m using XP so I use the command prompt. cd into the directory containing the images and type:
egg-texture-cards -o mainScreen.egg splash.png new.png newOver.png
This left an egg file in the folder called mainScreen.egg. Note that this egg needs to be in the same folder as the images if you want the images to appear on the geoms created.
The following code loads the egg and provides the basics for a GUI:
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.gui.DirectGui import *
import sys
class Main(DirectObject):
def __init__(self):
self.accept("escape", sys.exit)
self.setupScreen()
#This method is called when the button is clicked
def newButtonClicked(self):
print "Do the stuff you want when the new button is clicked"
#The screen is simply a background image and a single textured button
def setupScreen(self):
#All the textures are pulled from the premade egg-texture-card
self.screenImages=loader.loadModel('./textures/mainScreen.egg')
#find the splash image to act as background
self.splash=self.screenImages.find('**/splash')
#fiddle fiddle with the x and z values till the image fits the window.
self.splash.setScale(3,1,2)
#reparent the image to aspect2d
self.splash.reparentTo(aspect2d)
#The directButton accepts 4 states, normal, click, rollOver, and disabled.
self.newButton=DirectButton(geom=(self.screenImages.find('**/new'),
self.screenImages.find('**/new'),
self.screenImages.find('**/newOver'),
self.screenImages.find('**/new')))
#parent the button to the splash screen. Do it here so the fiddles are done respective to the splashscree
self.newButton.reparentTo(self.splash)
#Another syntax for setting a direct object's properties. This gets rid of the default bevel around the button
self.newButton['frameVisibleScale']=(0,0)
#fiddle fiddle to set the scale (as above)
self.newButton.setScale(0.15,1,0.04)
#fiddle fiddle to set the position using the x and z values.
#You could also position the background image the same way
self.newButton.setPos(0.34,0,0.04)
#Tell the Panda what method to call when the button is clicked
self.newButton['command']=self.newButtonClicked
Main()
run()
Hope this helps.