最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dice - How to display text on button press in Python Arcade? - Stack Overflow

programmeradmin2浏览0评论

I am making a dice roller program with Arcade. I have my buttons set up to print the result of rand.int, but I would like to make the result actually pop up in the Arcade window, instead of in the terminal. I am also unsure if I did the background correctly,I had to draw the ui manager after it at the end. Additionally, if anyone has advice on simplification, let me know! Thank you!!

import arcade
import arcade.gui
from random import randint

class QuitButton(arcade.gui.UIFlatButton):
    def on_click(self, event: arcade.gui.UIOnClickEvent):
        arcade.exit()

class MyWindow(arcade.Window):
    def __init__(self):
        super().__init__(800, 600, "DICE ROLLER", resizable=True)

        self.background = arcade.load_texture("galaxy.jpg")

        # --- Required for all code that uses UI element,
        # a UIManager to handle the UI.
        self.manager = arcade.gui.UIManager()
        self.manager.enable()

        # Set background color
        

        default_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 15,
            "font_color": arcade.color.WHITE,
            "border_width": 4,
            "border_color": None,
            "bg_color": (21, 19, 21),

            # used if button is pressed
            "bg_color_pressed": arcade.color.WHITE,
            "border_color_pressed": arcade.color.WHITE,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        pink_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.CHERRY_BLOSSOM_PINK,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.CHERRY_BLOSSOM_PINK,
            "border_color_pressed": arcade.color.CHERRY_BLOSSOM_PINK,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        blue_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.BABY_BLUE_EYES,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.BABY_BLUE_EYES,
            "border_color_pressed": arcade.color.BABY_BLUE_EYES,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        green_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.LIGHT_MOSS_GREEN,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.LIGHT_MOSS_GREEN,
            "border_color_pressed": arcade.color.LIGHT_MOSS_GREEN,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        purple_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.BRIGHT_UBE,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.BRIGHT_UBE,
            "border_color_pressed": arcade.color.BRIGHT_UBE,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        yellow_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.LEMON_CHIFFON,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.LEMON_CHIFFON,
            "border_color_pressed": arcade.color.LEMON_CHIFFON,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        orange_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.RAJAH,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.RAJAH,
            "border_color_pressed": arcade.color.RAJAH,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        # Create a vertical BoxGroup to align buttons
        self.v_box = arcade.gui.UIBoxLayout(x=200,y=200)

        # Create the buttons
        d20_button = arcade.gui.UIFlatButton(text="D20", width=200, style=blue_style)
        self.v_box.add(d20_button.with_space_around(bottom=20))

        d12_button = arcade.gui.UIFlatButton(text="D12", width=200, style=pink_style)
        self.v_box.add(d12_button.with_space_around(bottom=20))

        d10_button = arcade.gui.UIFlatButton(text="D10", width=200,style=green_style)
        self.v_box.add(d10_button.with_space_around(bottom=20))

        self.v2_box = arcade.gui.UIBoxLayout()

        d8_button = arcade.gui.UIFlatButton(text="D8", width=200, style=purple_style)
        self.v2_box.add(d8_button.with_space_around(bottom=20))

        d6_button = arcade.gui.UIFlatButton(text="D6", width=200, style=yellow_style)
        self.v2_box.add(d6_button.with_space_around(bottom=20))

        d4_button = arcade.gui.UIFlatButton(text="D4", width=200, style=orange_style)
        self.v2_box.add(d4_button.with_space_around(bottom=20))

        self.v3_box = arcade.gui.UIBoxLayout()

        # Again, method 1. Use a child class to handle events.
        quit_button = QuitButton(text="Quit", width=200, style=default_style)
        self.v3_box.add(quit_button)

        # --- Method 2 for handling click events,
        # assign self.on_click_start as callback
        d20_button.on_click = self.on_click_d20
        d12_button.on_click = self.on_click_d12
        d10_button.on_click = self.on_click_d10
        d8_button.on_click = self.on_click_d8
        d6_button.on_click = self.on_click_d6
        d4_button.on_click = self.on_click_d4


        # Create a widget to hold the v_box widget, that will center the buttons
        self.manager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x="left",
                align_x=185,
                anchor_y="center_y",
                align_y=25,
                child=self.v_box)
        )
        self.manager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x="right",
                align_x=-185,
                anchor_y="center_y",
                align_y=25,
                child=self.v2_box)
        )

        self.manager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x="center_x",
                anchor_y="center_y",
                align_y=-150,
                child=self.v3_box)
        )

    def on_click_d20(self, event):
        print(randint(1,20))  

    def on_click_d12(self, event):
        print(randint(1,12))

    def on_click_d10(self, event):
        print(randint(1,10))

    def on_click_d8(self, event):
        print(randint(1,8))

    def on_click_d6(self, event):
        print(randint(1,6))

    def on_click_d4(self, event):
        print(randint(1,4))

    def on_draw(self):
            self.clear()
            arcade.start_render()
        # Drawing the background image
            arcade.draw_texture_rectangle(400, 300, 800,600, self.background)
            self.manager.draw()
window = MyWindow()
arcade.run()

I am making a dice roller program with Arcade. I have my buttons set up to print the result of rand.int, but I would like to make the result actually pop up in the Arcade window, instead of in the terminal. I am also unsure if I did the background correctly,I had to draw the ui manager after it at the end. Additionally, if anyone has advice on simplification, let me know! Thank you!!

import arcade
import arcade.gui
from random import randint

class QuitButton(arcade.gui.UIFlatButton):
    def on_click(self, event: arcade.gui.UIOnClickEvent):
        arcade.exit()

class MyWindow(arcade.Window):
    def __init__(self):
        super().__init__(800, 600, "DICE ROLLER", resizable=True)

        self.background = arcade.load_texture("galaxy.jpg")

        # --- Required for all code that uses UI element,
        # a UIManager to handle the UI.
        self.manager = arcade.gui.UIManager()
        self.manager.enable()

        # Set background color
        

        default_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 15,
            "font_color": arcade.color.WHITE,
            "border_width": 4,
            "border_color": None,
            "bg_color": (21, 19, 21),

            # used if button is pressed
            "bg_color_pressed": arcade.color.WHITE,
            "border_color_pressed": arcade.color.WHITE,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        pink_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.CHERRY_BLOSSOM_PINK,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.CHERRY_BLOSSOM_PINK,
            "border_color_pressed": arcade.color.CHERRY_BLOSSOM_PINK,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        blue_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.BABY_BLUE_EYES,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.BABY_BLUE_EYES,
            "border_color_pressed": arcade.color.BABY_BLUE_EYES,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        green_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.LIGHT_MOSS_GREEN,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.LIGHT_MOSS_GREEN,
            "border_color_pressed": arcade.color.LIGHT_MOSS_GREEN,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        purple_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.BRIGHT_UBE,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.BRIGHT_UBE,
            "border_color_pressed": arcade.color.BRIGHT_UBE,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        yellow_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.LEMON_CHIFFON,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.LEMON_CHIFFON,
            "border_color_pressed": arcade.color.LEMON_CHIFFON,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        orange_style = {
            "font_name": "Kenney Mini Square",
            "font_size": 23,
            "font_color": arcade.color.RAJAH,
            "border_width": 4,
            "border_color": None,
            "bg_color": arcade.color.BLACK,

            # used if button is pressed
            "bg_color_pressed": arcade.color.RAJAH,
            "border_color_pressed": arcade.color.RAJAH,  # also used when hovered
            "font_color_pressed": arcade.color.BLACK,
        }

        # Create a vertical BoxGroup to align buttons
        self.v_box = arcade.gui.UIBoxLayout(x=200,y=200)

        # Create the buttons
        d20_button = arcade.gui.UIFlatButton(text="D20", width=200, style=blue_style)
        self.v_box.add(d20_button.with_space_around(bottom=20))

        d12_button = arcade.gui.UIFlatButton(text="D12", width=200, style=pink_style)
        self.v_box.add(d12_button.with_space_around(bottom=20))

        d10_button = arcade.gui.UIFlatButton(text="D10", width=200,style=green_style)
        self.v_box.add(d10_button.with_space_around(bottom=20))

        self.v2_box = arcade.gui.UIBoxLayout()

        d8_button = arcade.gui.UIFlatButton(text="D8", width=200, style=purple_style)
        self.v2_box.add(d8_button.with_space_around(bottom=20))

        d6_button = arcade.gui.UIFlatButton(text="D6", width=200, style=yellow_style)
        self.v2_box.add(d6_button.with_space_around(bottom=20))

        d4_button = arcade.gui.UIFlatButton(text="D4", width=200, style=orange_style)
        self.v2_box.add(d4_button.with_space_around(bottom=20))

        self.v3_box = arcade.gui.UIBoxLayout()

        # Again, method 1. Use a child class to handle events.
        quit_button = QuitButton(text="Quit", width=200, style=default_style)
        self.v3_box.add(quit_button)

        # --- Method 2 for handling click events,
        # assign self.on_click_start as callback
        d20_button.on_click = self.on_click_d20
        d12_button.on_click = self.on_click_d12
        d10_button.on_click = self.on_click_d10
        d8_button.on_click = self.on_click_d8
        d6_button.on_click = self.on_click_d6
        d4_button.on_click = self.on_click_d4


        # Create a widget to hold the v_box widget, that will center the buttons
        self.manager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x="left",
                align_x=185,
                anchor_y="center_y",
                align_y=25,
                child=self.v_box)
        )
        self.manager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x="right",
                align_x=-185,
                anchor_y="center_y",
                align_y=25,
                child=self.v2_box)
        )

        self.manager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x="center_x",
                anchor_y="center_y",
                align_y=-150,
                child=self.v3_box)
        )

    def on_click_d20(self, event):
        print(randint(1,20))  

    def on_click_d12(self, event):
        print(randint(1,12))

    def on_click_d10(self, event):
        print(randint(1,10))

    def on_click_d8(self, event):
        print(randint(1,8))

    def on_click_d6(self, event):
        print(randint(1,6))

    def on_click_d4(self, event):
        print(randint(1,4))

    def on_draw(self):
            self.clear()
            arcade.start_render()
        # Drawing the background image
            arcade.draw_texture_rectangle(400, 300, 800,600, self.background)
            self.manager.draw()
window = MyWindow()
arcade.run()

Share Improve this question asked Jan 20 at 0:29 Emily AEmily A 332 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Simple example:

import arcade
import random
from arcade.gui import UIAnchorLayout, UIFlatButton, UIGridLayout, UIView


class MyView(UIView):
    def __init__(self):
        super().__init__()
        grid = UIGridLayout(column_count=2, row_count=3, vertical_spacing=20,horizontal_spacing=30)

        self.ui.add(UIAnchorLayout(children=[grid]))
        self.btn_d20 = grid.add(UIFlatButton(text='D20'), row=0, column=0)
        self.btn_d20.on_click = self.on_click_d20
        self.btn_d12 = grid.add(UIFlatButton(text='D12'), row=1, column=0)
        self.btn_d12.on_click = self.on_click_d12
        self.btn_d10 = grid.add(UIFlatButton(text='D10'), row=2, column=0)
        self.btn_d10.on_click = self.on_click_d10
        self.btn_d8 = grid.add(UIFlatButton(text='D8'), row=0, column=1)
        self.btn_d8.on_click = self.on_click_d8
        self.btn_d6 = grid.add(UIFlatButton(text='D6'), row=1, column=1)
        self.btn_d6.on_click = self.on_click_d6
        self.btn_d4 = grid.add(UIFlatButton(text='D4'), row=2, column=1)
        self.btn_d4.on_click = self.on_click_d4

    def on_click_d20(self, event):
        self.btn_d20.text = str(random.randint(1, 20))

    def on_click_d12(self, event):
        self.btn_d12.text = str(random.randint(1, 12))

    def on_click_d10(self, event):
        self.btn_d10.text = str(random.randint(1, 10))

    def on_click_d8(self, event):
        self.btn_d8.text = str(random.randint(1, 8))

    def on_click_d6(self, event):
        self.btn_d6.text = str(random.randint(1, 6))

    def on_click_d4(self, event):
        self.btn_d4.text = str(random.randint(1, 4))


window = arcade.Window(width=800, height=600)
window.show_view(MyView())
window.run()

发布评论

评论列表(0)

  1. 暂无评论