我目前正在开展一个项目,我必须有一个由点组成的网格...... 似乎该类的代码是正确的,因为没有错误消息。 可能是因为你无法在课堂内创建画布吗? 谢谢你的帮助。 这是代码:
from tkinter import *
root = Tk()
class grid():
def createcanvas(self, master, width, height):
self.canvas = Canvas(master, width=width, height=height)
return(self.canvas)
def createlabel(self, master, textu):
self.l = Label(self.c, text=textu)
self.l.pack()
def createpoints(self, master, gridsize): # gridsize format '8X8'
gridsize = gridsize.split('X')
for i in range(len(gridsize)):
gridsize[i] = int(gridsize[i]) #[8,8] for example --> x,y coordinates
spx = 0
spy = int(canvas['height'])
for i in range(gridsize[1]):
for j in range(gridsize[0]):
master.create_oval(spx, spy, spx+5, spy+5)
spx = spx + (int(canvas['height'])/gridsize[0])
spx = 0
spy = spy - (int(canvas['height'])/gridsize[1])
canvas = grid()
canvas = canvas.createcanvas(root, 600, 600)
a = grid()
a.createpoints(canvas, '16X16')
root.mainloop()