如何在顶级Tkinter中创建形状

问题描述

每当我尝试在tkinter Toplevel中创建形状时,都会出现此错误消息:

Traceback (most recent call last):
  File "C:\Users\tkinter\__init__.py",line 1705,in __call__
    return self.func(*args)
  File "C:/Users/filea.py",line 183,in nxt_lvl
    paddle = Paddle(canvas,'blue')
  File "C:/Users/filea.py",line 125,in __init__
    self.id = canvas.create_rectangle(0,100,10,fill=color)
AttributeError: 'Toplevel' object has no attribute 'create_rectangle'

这是代码,您只需要关注Ball类和nxt_lvl函数

import random
import time

tk = Tk()
jump_counter = 0
You_win = Label(tk,text="You win!")


class Ball:
    def __init__(self,canvas,jumps,paddle,color):
        self.canvas = canvas
        self.paddle = paddle
        self.canvas_height = self.canvas.winfo_height()
        print(self.canvas_height)
        self.canvas_width = self.canvas.winfo_width()
        self.id = canvas.create_oval(10,25,fill=color)
        y = self.id
        self.canvas.move(self.id,245,100)
        self.x = 0
        self.y = 25
        self.jumps = jumps
        self.win = False
        self.gravity = 0.1
        self.canvas.bind_all('<KeyPress-Up>',self.turn_Up)
        self.canvas.bind_all('<keyrelease-Up>',self.turn_Up_stop)
        self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
        self.canvas.bind_all('<keyrelease-Left>',self.turn_left_stop)
        self.canvas.bind_all('<KeyPress-Right>',self.turn_right)
        self.canvas.bind_all('<keyrelease-Right>',self.turn_right_stop)

    def draw(self):
        global jump_counter
        paddle_pos = self.canvas.coords(self.paddle.id)
        paddle_p2s = self.canvas.coords(self.paddle.id2)
        paddle_p3s = self.canvas.coords(self.paddle.id3)
        self.canvas.move(self.id,self.x,self.y)
        pos = self.canvas.coords(self.id)
        self.y += self.gravity
        # pos[0] = left side of the Ball
        # pos[2] = Right side of the ball
        if pos[1] <= 0:  # Top of the Ball
            You_win.pack()
            self.win = True
            self.y = 3
        if pos[3] >= self.canvas_height:  # Bottom of the Ball
            jump_counter = 0
            self.y = 0

        if pos[0] <= 0:  # Left Side of the Ball
            self.x = 1

        if pos[2] >= self.canvas_width:  # Right Side of the Ball
            self.x = -1

        # --------------------------------Hit Collision--------------------------------#
        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if paddle_pos[1] <= pos[3] <= paddle_pos[3]:
                jump_counter = 0
                self.y = 0

        if pos[2] >= paddle_p2s[0] and pos[0] <= paddle_p2s[2]:
            if paddle_p2s[1] <= pos[3] <= paddle_p2s[3]:
                jump_counter = 0
                self.y = 0

        if pos[2] >= paddle_p3s[0] and pos[0] <= paddle_p3s[2]:
            if paddle_p3s[1] <= pos[3] <= paddle_p3s[3]:
                jump_counter = 0
                self.y = 0

        if pos[2] <= paddle_pos[2] and pos[0] >= paddle_pos[0]:
            if paddle_pos[1] <= pos[1] <= paddle_pos[3]:
                self.y = 1

        if pos[2] <= paddle_p2s[2] and pos[0] >= paddle_p2s[0]:
            if paddle_p2s[1] <= pos[1] <= paddle_p2s[3]:
                self.y = 1

        if pos[2] <= paddle_p3s[2] and pos[0] >= paddle_p3s[0]:
            if paddle_p3s[1] <= pos[1] <= paddle_p3s[3]:
                self.y = 1

    # If the top of the ball is greater than or equal too paddle's top and if the top of the ball is less than or equal too paddle's bottom

    # --------------------------------Hit Collision: End--------------------------------#

    def turn_Up(self,evt):
        global jump_counter
        if jump_counter == self.jumps:
            print(jump_counter)
            self.y += self.gravity
        elif jump_counter != self.jumps:
            print('Jump',jump_counter)
            jump_counter = jump_counter + 1
            self.y = -4

    def turn_Up_stop(self,evt):
        self.y += self.gravity

    def turn_left(self,evt):
        self.x = -3

    def turn_right(self,evt):
        self.x = 3

    def turn_left_stop(self,evt):
        self.x = self.x
        time.sleep(0.005)
        self.x = 0

    def turn_right_stop(self,evt):
        self.x = self.x
        time.sleep(0.005)
        self.x = 0


class Paddle:
    def __init__(self,color):
        pstartsX = [100,300,400]
        pstartsY = [125,215,300]
        random.shuffle(pstartsX)
        random.shuffle(pstartsY)
        self.canvas = canvas
        self.id = canvas.create_rectangle(0,fill=color)
        self.id2 = canvas.create_rectangle(0,fill=color)
        self.id3 = canvas.create_rectangle(0,fill=color)
        self.canvas.move(self.id,pstartsX[0],pstartsY[0])
        pstartsX.pop(0)
        pstartsY.pop(0)
        random.shuffle(pstartsX)
        random.shuffle(pstartsY)
        self.canvas.move(self.id2,pstartsY[0])
        pstartsX.pop(0)
        pstartsY.pop(0)
        random.shuffle(pstartsX)
        random.shuffle(pstartsY)
        self.canvas.move(self.id3,pstartsY[0])
        pstartsX.pop(0)
        pstartsY.pop(0)
        random.shuffle(pstartsX)
        random.shuffle(pstartsY)
        self.y = 0
        self.x = 0
        self.canvas_width = self.canvas.winfo_width()

    def draw(self):
        self.canvas.move(self.id,0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 1
        elif pos[2] >= self.canvas_width:
            self.x = -1


tkwidth = 500
tkheight = 400

tk.title('Level 1')
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas = Canvas(tk,width=tkwidth,height=tkheight,bd=0,highlightthickness=0)
canvas.pack()
tk.update()

paddle = Paddle(canvas,'blue')
ball = Ball(canvas,3,'green')

print(tkwidth,tkheight)


# width=tkwidth + 100,height=tkheight + 100,highlightthickness=0


def nxt_lvl():
    global pstartsX
    global pstartsY
    pstartsX = [100,400]
    pstartsY = [125,250,375]
    lvl2 = Toplevel(width=tkwidth + 100,highlightthickness=0)
    lvl2.title('Level 2')
    paddle = Paddle(lvl2,'blue')
    ball = Ball(lvl2,'green')
    ball.draw()


lvl_2_btn = Button(tk,text="Next Level?",command=nxt_lvl)

while 1:
    if not ball.win:
        ball.draw()
    elif ball.win:
        lvl_2_btn.pack()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

解决方法

考虑以下代码:

lvl2 = Toplevel(width=tkwidth + 100,height=tkheight + 100,bd=0,highlightthickness=0)
lvl2.title('Level 2')
paddle = Paddle(lvl2,'blue')
ball = Ball(lvl2,3,paddle,'green')

请注意,您正在lvl2传递给BallPaddle的地方,他们希望帆布。您需要在Toplevel内创建画布,并将画布传递到BallPaddle

它看起来应该像这样:

lvl2 = Toplevel(width=tkwidth + 100,highlightthickness=0)
lvl2.title('Level 2')
canvas = Canvas(lvl2)
canvas.pack(fill="both",expand=True)
paddle = Paddle(canvas,'blue')
ball = Ball(canvas,'green')

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...