| 1 |
import pyglet |
| 2 |
|
| 3 |
window = pyglet.window.Window(540, 500, caption="Widget Example") |
| 4 |
batch = pyglet.graphics.Batch() |
| 5 |
pyglet.gl.glClearColor(0.8, 0.8, 0.8, 1.0) |
| 6 |
|
| 7 |
|
| 8 |
@window.event |
| 9 |
def on_draw(): |
| 10 |
window.clear() |
| 11 |
batch.draw() |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
unpressed = pyglet.resource.image('button_unpressed.png') |
| 19 |
pressed = pyglet.resource.image('button_pressed.png') |
| 20 |
hover = pyglet.resource.image('button_hover.png') |
| 21 |
bar = pyglet.resource.image('bar.png') |
| 22 |
knob = pyglet.resource.image('knob.png') |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
def slider_handler(widget, value): |
| 30 |
slider_label.text = f"Slider Value: {round(value, 1)}" |
| 31 |
|
| 32 |
|
| 33 |
def toggle_button_handler(widget, value): |
| 34 |
toggle_label.text = f"Toggle Button: {value}" |
| 35 |
|
| 36 |
|
| 37 |
def push_button_handler(widget): |
| 38 |
push_label.text = "Push Button: True" |
| 39 |
|
| 40 |
|
| 41 |
def release_button_handler(widget): |
| 42 |
push_label.text = "Push Button: False" |
| 43 |
|
| 44 |
|
| 45 |
def text_entry_handler(widget, text): |
| 46 |
text_entry_label.text = f"Text: {text}" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
frame = pyglet.gui.Frame(window, order=4) |
| 56 |
|
| 57 |
|
| 58 |
togglebutton = pyglet.gui.ToggleButton(100, 400, pressed=pressed, unpressed=unpressed, hover=hover, batch=batch) |
| 59 |
togglebutton.set_handler('on_toggle', toggle_button_handler) |
| 60 |
frame.add_widget(togglebutton) |
| 61 |
toggle_label = pyglet.text.Label("Toggle Button: False", x=300, y=400, batch=batch, color=(0, 0, 0, 255)) |
| 62 |
|
| 63 |
|
| 64 |
pushbutton = pyglet.gui.PushButton(100, 300, pressed=pressed, unpressed=unpressed, hover=hover, batch=batch) |
| 65 |
pushbutton.set_handler('on_press', push_button_handler) |
| 66 |
pushbutton.set_handler('on_release', release_button_handler) |
| 67 |
frame.add_widget(pushbutton) |
| 68 |
push_label = pyglet.text.Label("Push Button: False", x=300, y=300, batch=batch, color=(0, 0, 0, 255)) |
| 69 |
|
| 70 |
|
| 71 |
slider = pyglet.gui.Slider(100, 200, bar, knob, edge=5, batch=batch) |
| 72 |
slider.set_handler('on_change', slider_handler) |
| 73 |
frame.add_widget(slider) |
| 74 |
slider_label = pyglet.text.Label("Slider Value: 0.0", x=300, y=200, batch=batch, color=(0, 0, 0, 255)) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
text_entry = pyglet.gui.TextEntry("Enter Your Name", 100, 100, 150, batch=batch) |
| 81 |
window.push_handlers(text_entry) |
| 82 |
text_entry.set_handler('on_commit', text_entry_handler) |
| 83 |
text_entry_label = pyglet.text.Label("Text: None", x=300, y=100, batch=batch, color=(0, 0, 0, 255)) |
| 84 |
|
| 85 |
|
| 86 |
pyglet.app.run() |