Как мне ссылаться на мою оконную переменную в отдельном классе? - PullRequest
1 голос
/ 01 мая 2020

Я использую библиотеку python arcade и постоянно получаю сообщение об ошибке «NameError: name 'window' notfined». Я попытался удалить основную функцию и просто запустить

window = MyGame() for button in window.buttonList: button.draw() arcade.run() без функции вокруг нее, но теперь мне нужно запустить это из другого файла без использования os.exe c или подпроцесса, хотя я все еще могу go вернитесь и запустите первый. Мне нужно использовать основную функцию, но я не знаю, как это сделать, не вызывая ошибки. Вот мой код

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...