+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何在顶级Tkinter中创建形状

发布于2021-04-12 14:52     阅读(151)     评论(0)     点赞(20)     收藏(4)


0

1

2

3

4

每当我尝试在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, 0, 100, 10, fill=color)
AttributeError: 'Toplevel' object has no attribute 'create_rectangle'

这是代码,您只需要关注BallClass和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, 10, 25, 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, canvas, color):
        pstartsX = [100, 300, 400]
        pstartsY = [125, 215, 300]
        random.shuffle(pstartsX)
        random.shuffle(pstartsY)
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
        self.id2 = canvas.create_rectangle(0, 0, 100, 10, fill=color)
        self.id3 = canvas.create_rectangle(0, 0, 100, 10, 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, pstartsX[0], pstartsY[0])
        pstartsX.pop(0)
        pstartsY.pop(0)
        random.shuffle(pstartsX)
        random.shuffle(pstartsY)
        self.canvas.move(self.id3, pstartsX[0], 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, self.x, 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, paddle, 'green')

print(tkwidth, tkheight)


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


def nxt_lvl():
    global pstartsX
    global pstartsY
    pstartsX = [100, 300, 400]
    pstartsY = [125, 250, 375]
    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')
    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')

注意,你是传递lvl2BallPaddle在他们希望在画布的地方。您需要在中创建画布Toplevel,并将画布传递给BallPaddle

它看起来应该像这样:

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

0

1

2

3

4

5



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/936027/8aa2e3d7ab7b350f939b/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

20 0
收藏该文
已收藏

评论内容:(最多支持255个字符)