OcempGUI FAQ
Frequently asked questions regarding OcempGUI and its modules. If you have trouble installing or running OcempGUI, this is the wrong place. Please go to the OcempGUI Help requests page instead.
If you have a question you would like to have answered, just add it at the bottom of the page, add it to the Open Questions.
Renderer integration into own pygame loop
1 import sys, random, pygame
2 from ocempgui.widgets import Button, Renderer, Constants
3 # Used to see some action happending in the GUI.
4 amount = 0
5 def _count_clicks (b):
6 global amount
7 amount += 1
8 b.text = "Clicked %d times" % amount
9 # Initialize pygame window
10 pygame.init ()
11 screen = pygame.display.set_mode ((200, 200))
12 screen.fill ((255, 200, 100))
13 # Create the Renderer to use for the UI elements.
14 re = Renderer ()
15 # Bind it to a part of the screen, which it will use to draw the widgets.
16 # Here we use the complete screen.
17 re.screen = screen
18 # Create a button, place it at x=10, y=30, bind a callback to its
19 # clicking action and add it to the Renderer instance.
20 button = Button ("Simple Button")
21 button.topleft = 10, 30
22 button.connect_signal (Constants.SIG_CLICKED, _count_clicks, button)
23 re.add_widget (button)
24 # Some variables we will need in the main loop for drawing a rect.
25 rnd = None
26 color = None
27 cnt = 100
28 while True:
29 events = pygame.event.get ()
30 for ev in events:
31 if ev.type == pygame.QUIT:
32 sys.exit ()
33 # We could handle other events separately, too, but do not care.
34 # Draw the rectangle on the screen.
35 cnt -= 1
36 if cnt == 0:
37 rnd = (random.randint (0, 5), random.randint (0, 5), random.randint (0, 5))
38 color = (rnd[0] * 340) % 255, (rnd[1] * 122) % 255, (rnd[2] * 278) % 255
39 pygame.draw.rect (screen, color, (60, 50, 50, 50))
40 cnt = 100
41 # Pass all received events to the Renderer. We also could pass only
42 # a subset of them, but want it to get all.
43 re.distribute_events (*events)
44 # Force a refresh of the UI elements as our main screen changed and
45 # we want them to be placed upon the changes (look at the
46 # intersection with the rectangle).
47 re.refresh ()
48 # We do not need to flip as it is done in the refresh() method of
49 # the Renderer. If we assign just a part of the screen to the
50 # Renderer, we would have to, of course.
51 # pygame.display.flip ()
52 # Do not use 100% CPU.
53 pygame.time.delay (15)
Open Questions
Open questions to be answered.
Is it possible to create a Label widget with no background ?