【问题标题】:Trouble filling a Python tkinter/turtle window with squares无法用正方形填充 Python tkinter/turtle 窗口
【发布时间】:2018-10-21 03:31:28
【问题描述】:

我无法让 turtle.begin_fill() / turtle.end_fill() 命令在 tkinter 窗口中工作。我想让程序以 64x32 网格在屏幕上绘制一堆彩色单元格。这是设置窗口和绘制的整个代码,但是在设置窗口后它根本不做任何事情:

import turtle as tu
import tkinter as tk

axis = []
num = -512

for i in range(0,64):
    axis.append(num)
    num = num + 16

def drawworld(*args):
    for i in range(0,64):
        for j in range(0,32):
            tu.goto(axis[i],axis[j]+256)
            tu.pd()
            tu.color=("#ffff00")
            tu.begin_fill()
            for k in range(0,4):
                tu.fd(16)
                tu.lt(90)
            tu.end_fill()
            tu.pu()

root = tk.Tk()
root.title("game")
root.resizable(False, False)
canvas = tk.Canvas(master = root, width = 1024, height = 512)
canvas.grid(row=0, column=0, columnspan=10)
tu = tu.RawTurtle(canvas)
tu.speed(50)
tu.pu()
tu.ht()

drawworld()

root.mainloop()

但是,如果我要注释掉填充线,代码可以完美运行(没有为框着色),这意味着一定有问题:

tu.color=("#ffff00")
tu.begin_fill()
for k in range(0,4):
    tu.fd(16)
    tu.lt(90)
tu.end_fill()
tu.pu()

我查看了文档,这应该是完美的语法。我做错了什么?

【问题讨论】:

    标签: python-3.x turtle-graphics tkinter-canvas


    【解决方案1】:

    我相信你的问题是这一行:

    tu.color=("#ffff00")
    

    应该是这样的:

    tu.color("#ffff00")
    

    要设置颜色,您无需指定海龟的颜色属性,而是调用海龟的.color() 方法。您的代码进行了此更改和其他一些清理工作:

    import tkinter as tk
    from turtle import RawTurtle
    
    axis = []
    num = -512
    
    for _ in range(64):
        axis.append(num)
        num += 16
    
    def drawworld():
        for i in range(64):
            for j in range(32):
                tu.goto(axis[i], axis[j] + 256)
                tu.pendown()
                tu.color("#ffff00")
                tu.begin_fill()
                for _ in range(4):
                    tu.forward(16)
                    tu.left(90)
                tu.end_fill()
                tu.penup()
    
    root = tk.Tk()
    root.title("game")
    root.resizable(False, False)
    
    canvas = tk.Canvas(master=root, width=1024, height=512)
    canvas.grid(row=0, column=0, columnspan=10)
    
    tu = RawTurtle(canvas)
    tu.speed('fastest')
    tu.hideturtle()
    tu.penup()
    
    drawworld()
    
    root.mainloop()
    

    但是,这是一种非常缓慢的方法。有更快的方法,这里有一种使用冲压来加快进程的方法:

    import tkinter as tk
    from turtle import RawTurtle
    from random import random
    
    CUBE_SIZE = 16
    CURSOR_SIZE = 20
    WIDTH, HEIGHT = 1024, 512
    
    axis = []
    num = -HEIGHT
    
    for _ in range(WIDTH // CUBE_SIZE):
        axis.append(num)
        num += CUBE_SIZE
    
    def drawworld():
        for i in range(WIDTH // CUBE_SIZE):
            tu.goto(axis[i] + CUBE_SIZE // 2, axis[0] + HEIGHT // 2 + CUBE_SIZE // 2)
    
            for j in range(HEIGHT // CUBE_SIZE):
                tu.color(random(), random(), random())
                tu.stamp()
                tu.forward(CUBE_SIZE)
    
    root = tk.Tk()
    root.title("game")
    root.resizable(False, False)
    
    canvas = tk.Canvas(master=root, width=WIDTH, height=HEIGHT)
    canvas.grid(row=0, column=0, columnspan=10)
    
    tu = RawTurtle(canvas)
    tu.shape('square')
    tu.shapesize(CUBE_SIZE / CURSOR_SIZE)
    tu.speed('fastest')
    tu.hideturtle()
    tu.setheading(90)
    tu.penup()
    
    drawworld()
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 2018-06-15
      相关资源
      最近更新 更多