如何在单独的类中引用我的窗口变量?
Posted
技术标签:
【中文标题】如何在单独的类中引用我的窗口变量?【英文标题】:How do I reference my window variable in a separate class? 【发布时间】:2020-08-15 12:44:43 【问题描述】:我正在使用 python 的街机库,并且不断收到“NameError: name 'window' is not defined”错误。我尝试删除主要功能并运行
window = MyGame()
for button in window.buttonList:
button.draw()
arcade.run()
没有它周围的功能,但现在我需要从另一个文件运行它而不使用 os.exec 或子进程,同时仍然能够返回并运行第一个文件。我需要使用 main 函数,但我不知道如何在不引发错误的情况下执行此操作。这是我的代码
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】:您可以在单独的课程中使用arcade.View
代替arcade.Window
。示例:
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()
输出:
【讨论】:
以上是关于如何在单独的类中引用我的窗口变量?的主要内容,如果未能解决你的问题,请参考以下文章