The ImageMap is a widget, that can
display any type of image, pygame can handle, and supports
different mouse events.
In contrast to the ImageButton it does
not react visually upon those events, but can store the last
event, that occured as well as providing the relative position
coordinates of the event.
To create an ImageMap, you have to
provide an image surface or filename similar to the
ImageButton constructor.
imagemap = ImageMap ("path/to/an/image.png")
imagemap = ImageMap (pygame_surface)
set_picture() method.
Below you will find an example of the
ImageMap widget class.
You can find the following example as a python script under
examples/imagemap.py.
# ImageMap examples.
import os
from ocempgui.widgets import *
from ocempgui.widgets.Constants import *
def _get_type (eventtype):
if eventtype == SIG_MOUSEDOWN:
return "SIG_MOUSEDOWN"
elif eventtype == SIG_MOUSEUP:
return "SIG_MOUSEUP"
elif eventtype == SIG_MOUSEMOVE:
return "SIG_MOUSEMOVE"
else:
return "Unknown signal"
def _got_mouseevent (event, imagemap, labels):
labels[0].text = "Signal: %s" % _get_type (imagemap.last_event.signal)
if imagemap.last_event.signal != SIG_MOUSEMOVE:
labels[1].text = "Button: %d" % imagemap.last_event.data.button
else:
labels[1].text = "Button: None"
labels[2].text = "Event pos: %s" % str (imagemap.last_event.data.pos)
labels[3].text = "Rel. pos: %s" % str (imagemap.relative_position)
def _create_vframe (text):
frame = VFrame (Label (text))
frame.spacing = 5
frame.align = ALIGN_LEFT
return frame
def create_imagemap_view ():
frm_map = _create_vframe ("ImageMap")
imagemap = ImageMap ("image.png")
lbl_desc = Label ("Move the mouse over the ImageMap and" + os.linesep +
"press the mouse buttons to interact with it.")
lbl_desc.multiline = True
lbl_results = [Label ("Signal:"), Label ("Button:"), Label ("Event pos:"),
Label ("Rel. pos:")]
for label in lbl_results:
label.get_style()["fgcolor"][STATE_NORMAL] = (255, 0, 0)
imagemap.connect_signal (SIG_MOUSEDOWN, _got_mouseevent, imagemap,
lbl_results)
imagemap.connect_signal (SIG_MOUSEMOVE, _got_mouseevent, imagemap,
lbl_results)
imagemap.connect_signal (SIG_MOUSEUP, _got_mouseevent, imagemap,
lbl_results)
frm_map.add_child (imagemap, lbl_desc, *lbl_results)
return frm_map
if __name__ == "__main__":
# Initialize the drawing window.
re = Renderer ()
re.create_screen (550, 470)
re.title = "ImageMap examples"
re.color = (234, 228, 223)
re.add_widget (create_imagemap_view ())
# Start the main rendering loop.
re.start ()
Example 36. ImageMap example
The ProgressBar is a non-interactive
widget class, that uses a horizontal bar to display operation
efforts. Its value range reaches from 0.0 to 100.0.
To create a ProgressBar widget, you
simply will invoke its constructor with no arguments.
bar = ProgressBar ()
Similar to the Range widget class, the
ProgressBar supports the
step and
set_step() methods to set the step
range for continouos increments/decrements
bar.step = 1.4
bar.set_step (10)
# Increment/decrement the bar value by the currently set step range.
while process_runs:
bar.increment () # or bar.decrement ()
...
bar.value = 50.0
You can optionally display a short amount of text centered on it,
which can be set with the text attribute or
set_text() method.
bar.text = "Processing data..."
bar.set_text ("Please wait...")
Below you will find an example of the
ProgressBar widget class.
You can find the following example as a python script under
examples/progressbar.py.
# ProgressBar examples.
from ocempgui.widgets import *
from ocempgui.widgets.Constants import *
def _update_bar (bar, button):
if bar.value == 0:
while bar.value < 100:
bar.increase ()
bar.manager.force_update ()
button.text = "Clean the progressbar"
else:
while bar.value > 0:
bar.decrease ()
bar.manager.force_update ()
button.text = "Fill the progressbar"
def _update_text (bar):
bar.text = "%.2f" % bar.value + "%"
def _create_vframe (text):
frame = VFrame (Label (text))
frame.spacing = 5
frame.align = ALIGN_LEFT
return frame
def create_progressbar_view ():
table = Table (1, 3)
table.spacing = 5
# Create and display a simple ProgressBar.
frame = _create_vframe ("ProgressBar")
progress = ProgressBar ()
progress.step = 0.5
# Create a button to start filling.
btn = Button ("#Fill the ProgressBar")
btn.connect_signal ("clicked", _update_bar, progress, btn)
frame.add_child (progress, btn)
table.add_child (0, 0, frame)
# ProgressBar with text.
frame = _create_vframe ("Progressbar with text")
progress = ProgressBar ()
progress.text = "0.00%"
progress.step = 0.5
progress.connect_signal (SIG_VALCHANGED, _update_text, progress)
# Create a button to start filling.
btn = Button ("Fill the ProgressBar")
btn.connect_signal ("clicked", _update_bar, progress, btn)
frame.add_child (progress, btn)
table.add_child (0, 1, frame)
# Insensitive progressbar.
frame = _create_vframe ("Insensitive Progressbar")
progress = ProgressBar ()
progress.value = 50.0
progress.text = "50.00%"
progress.sensitive = False
frame.add_child (progress)
table.add_child (0, 2, frame)
return table
if __name__ == "__main__":
# Initialize the drawing window.
re = Renderer ()
re.create_screen (450, 150)
re.title = "ProgressBar examples"
re.color = (234, 228, 223)
re.add_widget (create_progressbar_view ())
# Start the main rendering loop.
re.start ()
Example 37. ProgressBar example