图形用户界面小部件#

gui_widgets.py#
1"""
2Example code showing how to create some of the different UIWidgets.
3
4If Python and Arcade are installed, this example can be run from the command line with:
5python -m arcade.examples.gui_widgets
6"""
7import arcade
8import arcade.gui
9import arcade.gui.widgets.buttons
10import arcade.gui.widgets.layout
11import arcade.gui.widgets.text
12
13
14class MyView(arcade.View):
15 def __init__(self):
16 super().__init__()
17 # --- Required for all code that uses UI element,
18 # a UIManager to handle the UI.
19 self.ui = arcade.gui.UIManager()
20
21 # Create a vertical BoxGroup to align buttons
22 self.v_box = arcade.gui.widgets.layout.UIBoxLayout(space_between=20)
23
24 # Create a text label
25 ui_text_label = arcade.gui.widgets.text.UITextArea(
26 text="This is a Text Widget",
27 width=450,
28 height=40,
29 font_size=24,
30 font_name="Kenney Future",
31 )
32 self.v_box.add(ui_text_label)
33
34 text = (
35 "The real danger is not that computers will begin to think like people, "
36 "but that people will begin "
37 "to think like computers. - Sydney Harris (Journalist)"
38 )
39 ui_text_label = arcade.gui.widgets.text.UITextArea(
40 text=text, width=450, height=60, font_size=12, font_name="Arial"
41 )
42 self.v_box.add(ui_text_label)
43
44 # Create a UIFlatButton
45 ui_flatbutton = arcade.gui.widgets.buttons.UIFlatButton(
46 text="Flat Button", width=200
47 )
48 self.v_box.add(ui_flatbutton)
49
50 # Handle Clicks
51 @ui_flatbutton.event("on_click")
52 def on_click_flatbutton(event):
53 print("UIFlatButton pressed", event)
54
55 # Create a UITextureButton
56 texture = arcade.load_texture(":resources:onscreen_controls/flat_dark/play.png")
57 ui_texture_button = arcade.gui.widgets.buttons.UITextureButton(texture=texture)
58
59 # Handle Clicks
60 @ui_texture_button.event("on_click")
61 def on_click_texture_button(event):
62 print("UITextureButton pressed", event)
63
64 self.v_box.add(ui_texture_button)
65
66 # Create a widget to hold the v_box widget, that will center the buttons
67 self.ui.add(
68 arcade.gui.widgets.layout.UIAnchorLayout(children=[self.v_box])
69 )
70
71 def on_click_start(self, event):
72 print("Start:", event)
73
74 def on_show_view(self):
75 self.window.background_color = arcade.color.DARK_BLUE_GRAY
76 # Enable UIManager when view is shown to catch window events
77 self.ui.enable()
78
79 def on_hide_view(self):
80 # Disable UIManager when view gets inactive
81 self.ui.disable()
82
83 def on_draw(self):
84 self.clear()
85 self.ui.draw()
86
87
88if __name__ == '__main__':
89 window = arcade.Window(800, 600, "UIExample", resizable=True)
90 window.show_view(MyView())
91 window.run()