如何在 canvas tkinter 对象上显示网格?
How to display a grid on a canvas tkinter object?
所以我正在 python 中创建生命游戏,并想使用 python 的 tkinter class 在 canvas 之上创建一个网格].提前致谢。
为了制作网格,我使用了 canvas class 的 create_line() 函数,并使用 range() 函数进行循环,以便将我的线条绘制到canvas 的宽度和高度,使用两个单独的 for 循环,一个用于宽度,一个用于高度。我得到的循环取自 Whosebug 上的以下代码片段:
我以为我明白发生了什么,但我认为应该发生的事情却没有
from tkinter import *
from random import *
window = Tk()
window.title('Game Of Life')
canvas = Canvas(window, background='white', width=800, height=600)
def create_grid(canvas):
width = canvas.winfo_width() # gets width of the canvas
height = canvas.winfo_height() # gets height of the canvas
for line in range(0, width, 1): # range(start, stop, step)
canvas.create_line([(line, 0), (line, height)], fill='black', tags='grid_line_w')
for line in range(0, height, 1):
canvas.create_line([(0, line), (width, line)], fill='black', tags='grid_line_h')
create_grid(canvas)
canvas.grid(row=0, column=0)
window.mainloop()
我的预期结果是白色 canvas,垂直和水平线拉伸 canvas 的宽度和高度。但我的实际结果只是白色 canvas,顶部没有线条。
我不相信 winfo_width
和 winfo_height
在这种情况下给出准确的值。根据 https://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.winfo_height-method,如果您还没有 packed/gridded/placed 小部件,则功能只是 return 1,即使您有,如果 window的事件循环最近没有更新。你已经知道你想要宽度为800,高度为600,所以你也可以直接这样定义。
另一个问题是您的 range
调用的 step
参数为 1。这意味着线条将相隔一个像素,这将有效地将整个 canvas 涂成黑色.我建议迈大一步。
from tkinter import *
from random import *
window = Tk()
window.title('Game Of Life')
def create_grid(window):
width = 800
height = 600
canvas = Canvas(window, background='white', width=width, height=height)
for line in range(0, width, 10): # range(start, stop, step)
canvas.create_line([(line, 0), (line, height)], fill='black', tags='grid_line_w')
for line in range(0, height, 10):
canvas.create_line([(0, line), (width, line)], fill='black', tags='grid_line_h')
canvas.grid(row=0, column=0)
create_grid(window)
window.mainloop()
结果:
所以我正在 python 中创建生命游戏,并想使用 python 的 tkinter class 在 canvas 之上创建一个网格].提前致谢。
为了制作网格,我使用了 canvas class 的 create_line() 函数,并使用 range() 函数进行循环,以便将我的线条绘制到canvas 的宽度和高度,使用两个单独的 for 循环,一个用于宽度,一个用于高度。我得到的循环取自 Whosebug 上的以下代码片段:
from tkinter import *
from random import *
window = Tk()
window.title('Game Of Life')
canvas = Canvas(window, background='white', width=800, height=600)
def create_grid(canvas):
width = canvas.winfo_width() # gets width of the canvas
height = canvas.winfo_height() # gets height of the canvas
for line in range(0, width, 1): # range(start, stop, step)
canvas.create_line([(line, 0), (line, height)], fill='black', tags='grid_line_w')
for line in range(0, height, 1):
canvas.create_line([(0, line), (width, line)], fill='black', tags='grid_line_h')
create_grid(canvas)
canvas.grid(row=0, column=0)
window.mainloop()
我的预期结果是白色 canvas,垂直和水平线拉伸 canvas 的宽度和高度。但我的实际结果只是白色 canvas,顶部没有线条。
我不相信 winfo_width
和 winfo_height
在这种情况下给出准确的值。根据 https://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.winfo_height-method,如果您还没有 packed/gridded/placed 小部件,则功能只是 return 1,即使您有,如果 window的事件循环最近没有更新。你已经知道你想要宽度为800,高度为600,所以你也可以直接这样定义。
另一个问题是您的 range
调用的 step
参数为 1。这意味着线条将相隔一个像素,这将有效地将整个 canvas 涂成黑色.我建议迈大一步。
from tkinter import *
from random import *
window = Tk()
window.title('Game Of Life')
def create_grid(window):
width = 800
height = 600
canvas = Canvas(window, background='white', width=width, height=height)
for line in range(0, width, 10): # range(start, stop, step)
canvas.create_line([(line, 0), (line, height)], fill='black', tags='grid_line_w')
for line in range(0, height, 10):
canvas.create_line([(0, line), (width, line)], fill='black', tags='grid_line_h')
canvas.grid(row=0, column=0)
create_grid(window)
window.mainloop()
结果: