2

I'm using python's arcade library and I keep getting the "NameError: name 'window' is not defined" error. I tried deleting the main function and just running the

window = MyGame() for button in window.buttonList: button.draw() arcade.run() without the function around it, but now I need to run this from another file without using os.exec or subprocess while still being able to go back and run the first one. I need to use a main function, but I don't know how to do it without raising an error. Here is my code

from subprocess import call
from tkinter import *
from tkinter import filedialog

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Menu"


class MenuItem():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def draw(self):
        pass

    def func(self):
        pass


class FreeDraw(MenuItem):
    def func(self):
        window.close()
        window.set_visible(False)
        #run other file

    def draw(self):
        window.buttonShapes.append(arcade.create_rectangle_outline(self.x, self.y, self.width, self.height, arcade.color.BLACK))


class MyGame(arcade.Window):
    """ Our custom Window Class"""

    def __init__(self):
        """ Initializer """
        # Call the parent class initializer
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        self.buttonList = [FreeDraw(SCREEN_WIDTH/2, 100, 400, 100)]
        self.buttonShapes = arcade.ShapeElementList()

        arcade.set_background_color(arcade.color.ASH_GREY)

    def setup(self):
        pass

    def on_draw(self):
        """ Draw everything """
        arcade.start_render()
        arcade.draw_text("Free Draw", (self.buttonList[0].x - self.buttonList[0].width / 2) + 115,
                         self.buttonList[0].y - 25,
                         arcade.color.BLACK, 30)
        self.buttonShapes.draw()

    def on_key_press(self, key, modifiers):
        pass

    def on_key_release(self, key, modifiers):
        pass

    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
        pass

    def on_mouse_release(self, x: float, y: float, button: int,
                         modifiers: int):
        for button in self.buttonList:
            if x <= button.x + (button.width / 2) and x >= button.x - (button.width / 2):
                if y <= button.y + (button.height / 2) and y >= button.y - (button.height / 2):
                    button.func()
                    self.buttonList[0].func()

def main():
    window = MyGame()
    for button in window.buttonList:
        button.draw()
    arcade.run()

main()

1 Answers1

0

You can use arcade.View instead of arcade.Window in your separate classes. Example:

import arcade


class View1(arcade.View):

    def on_draw(self):
        arcade.start_render()
        arcade.draw_text('View 1', 300, 200, arcade.color.RED, font_size=30, anchor_x='center')

    def on_mouse_press(self, _x, _y, _button, _modifiers):
        self.window.show_view(View2())


class View2(arcade.View):

    def on_draw(self):
        arcade.start_render()
        arcade.draw_text('View 2', 300, 200, arcade.color.RED, font_size=30, anchor_x='center')

    def on_mouse_press(self, _x, _y, _button, _modifiers):
        self.window.show_view(View1())


window = arcade.Window(600, 400)
window.show_view(View1())
arcade.run()

Output:

Arcade View

Alderven
  • 7,569
  • 5
  • 26
  • 38