图形用户界面滑块#

gui_slider.py#
1"""
2GUI Slider Example
3
4If Python and Arcade are installed, this example can be run from the
5command line with:
6python -m arcade.examples.view_screens_minimal
7
8This example demonstrates how to create a GUI slider and react to
9changes in its value.
10
11There are two other ways of handling update events. For more
12information on this subject, see the gui_flat_button example.
13
14If Python and Arcade are installed, this example can be run from the command line with:
15python -m arcade.examples.gui_slider
16"""
17import arcade
18from arcade.gui import UIManager, UILabel
19from arcade.gui.events import UIOnChangeEvent
20from arcade.gui.widgets.slider import UISlider
21
22
23class MyView(arcade.View):
24 def __init__(self):
25 super().__init__()
26 # Required, create a UI manager to handle all UI widgets
27 self.ui = UIManager()
28
29 # Create our pair of widgets
30 ui_slider = UISlider(value=50, width=300, height=50)
31 label = UILabel(text=f"{ui_slider.value:02.0f}")
32
33 # Change the label's text whenever the slider is dragged
34 # See the gui_flat_button example for more information.
35 @ui_slider.event()
36 def on_change(event: UIOnChangeEvent):
37 label.text = f"{ui_slider.value:02.0f}"
38 label.fit_content()
39
40 # Create a layout to hold the label and the slider
41 ui_anchor_layout = arcade.gui.widgets.layout.UIAnchorLayout()
42 ui_anchor_layout.add(
43 child=ui_slider,
44 anchor_x="center_x",
45 anchor_y="center_y"
46 )
47 ui_anchor_layout.add(child=label, align_y=50)
48
49 self.ui.add(ui_anchor_layout)
50
51 def on_show_view(self):
52 self.window.background_color = arcade.color.DARK_BLUE_GRAY
53 # Enable UIManager when view is shown to catch window events
54 self.ui.enable()
55
56 def on_hide_view(self):
57 # Disable UIManager when view gets inactive
58 self.ui.disable()
59
60 def on_draw(self):
61 self.clear()
62 self.ui.draw()
63
64
65if __name__ == '__main__':
66 window = arcade.Window(800, 600, "UIExample", resizable=True)
67 window.show_view(MyView())
68 window.run()