【问题标题】:AttributeError: '_tkinter.tkapp' object has no attribute 'create_rectangle'AttributeError:“_tkinter.tkapp”对象没有属性“create_rectangle”
【发布时间】:2021-05-10 08:01:36
【问题描述】:

试图制作一个飞扬的鸟克隆,但在将成为鸟的物体中产卵时遇到了麻烦。我试图自己修复它,但我想不出它为什么不起作用。试图在网上查找是否有任何修复,但我一无所获。我不明白问题是什么以及为什么它没有产生。我想不出如何解决它。我的代码在这里。

import tkinter


class Menu:

    _BUTTON_HEIGHT = 2
    _BUTTON_WIDTH = 10
    _PLAY_BUTTON = "Start"
    _EXIT_BUTTON = "Exit"

    def __init__(self):
        """"""
        self.canvas = tkinter.Tk()
        self.play_button = None
        self.exit_button = None
        self.starting_game_new = None

    def canvas_create(self):
        """Creates a window for the frame to be displayed"""
        self.canvas.geometry("500x700")

    def menu_buttons(self):
        """"""
        self.play_button = tkinter.Button(self.canvas,
                                          command=self.start_game,
                                          text=Menu._PLAY_BUTTON,
                                          width=Menu._BUTTON_WIDTH,
                                          height=Menu._BUTTON_HEIGHT)
        self.play_button.pack()

        self.exit_button = tkinter.Button(self.canvas,
                                          command=self.exit_game,
                                          text=Menu._EXIT_BUTTON,
                                          width=Menu._BUTTON_WIDTH,
                                          height=Menu._BUTTON_HEIGHT)
        self.exit_button.pack()

    def start_game(self):
        self.play_button.pack_forget()
        self.exit_button.pack_forget()
        self.starting_game_new = GameFrame(canvas=self.canvas)
        self.starting_game_new.create_frame()
        self.starting_game_new.create_bird()

    def exit_game(self):
        self.canvas.destroy()


class GameFrame:

    _FRAME_HEIGHT = 700
    _FRAME_WIDTH = 500
    _FRAME_COLOUR = "dodger blue"
    _X_BIRD = 200
    _X_BIRD_2 = 250
    _Y_BIRD = 150
    _Y_BIRD_2 = 200
    _BIRD_COLOUR = "gold2"

    def __init__(self, canvas):
        """"""
        self.game_frame = None
        self.canvas = canvas
        self.bird = None

    def create_frame(self):
        """Creates the game canvas and background"""
        self.game_frame = tkinter.Canvas(self.canvas,
                                         width=GameFrame._FRAME_WIDTH,
                                         height=GameFrame._FRAME_HEIGHT,
                                         background=GameFrame._FRAME_COLOUR)
        self.game_frame.pack()

    def create_bird(self):
        self.bird = self.canvas.create_rectangle(GameFrame._X_BIRD,
                                                 GameFrame._Y_BIRD,
                                                 GameFrame._X_BIRD_2,
                                                 GameFrame._Y_BIRD_2,
                                                 fill=GameFrame._BIRD_COLOUR)


def main():
    start = Menu()
    start.canvas_create()
    start.menu_buttons()
    start.canvas.mainloop()


if __name__ == '__main__':
    main()

这里的错误代码:

Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“(文件)init.py”,第 1883 行,调用中返回 self.func(*args) 文件“”,行50、在start_game self.starting_game_new.create_bird()文件“D:/Downloads/flappybirdgame-main/aewf.py”,第82行,在create_bird self.bird = self.canvas.create_rectangle(GameFrame._X_BIRD, File "",第 2345 行,在 getattr 返回 getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'create_rectangle'

【问题讨论】:

  • 请发布整个错误代码。
  • Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“(file)__init__.py”,第 1883 行,在 call 中返回 self.func(*args)文件“”,第 50 行,在 start_game self.starting_game_new.create_bird() 文件“D:/Downloads/flappybirdgame-main/aewf.py”,第 82 行,在 create_bird self.bird = self.canvas.create_rectangle(GameFrame._X_BIRD , 文件 "", line 2345, in getattr return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'create_rectangle'
  • 错误是因为你说self.canvas = tkinter.Tk() 并且你把它作为你的画布传递。为什么?您需要传递一个画布,而不是 Tk() 窗口。

标签: python python-3.x tkinter


【解决方案1】:

create_rectangle 方法是画布上的一个方法。您的变量self.canvas 不是画布,它是根窗口。

您需要创建一个 tkinter Canvas 小部件才能绘制一个矩形。看起来你在这一行这样做:

self.game_frame = tkinter.Canvas(self.canvas, ...)

因此,您需要在此小部件上调用 create_rectangle 而不是 self.canvas

self.bird = self.game_frame.create_rectangle(...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 2020-08-20
    • 2022-11-03
    • 2012-12-01
    • 2021-04-19
    相关资源
    最近更新 更多