Python 处理.py空白窗口问题
我尝试使用processing.py编写一个A*alg,但代码开头有一个问题:我的窗口完全是空白的 因此,我希望出现一个网格,等待用户单击单元格,然后用黑色矩形填充该单元格。 但是,我只想在代码的开头运行它,所以我没有把它放在draw函数中 这是我的密码:Python 处理.py空白窗口问题,python,python-3.x,user-interface,2d,processing,Python,Python 3.x,User Interface,2d,Processing,我尝试使用processing.py编写一个A*alg,但代码开头有一个问题:我的窗口完全是空白的 因此,我希望出现一个网格,等待用户单击单元格,然后用黑色矩形填充该单元格。 但是,我只想在代码的开头运行它,所以我没有把它放在draw函数中 这是我的密码: taille = 400 pas = taille // 20 def setup(): size(taille, taille) background(255, 255, 255) stroke(0) st
taille = 400
pas = taille // 20
def setup():
size(taille, taille)
background(255, 255, 255)
stroke(0)
strokeWeight(2)
frameRate(20)
for i in range(pas, taille, pas):
line(i, 0, i, taille)
line(0, i, taille, i)
drawRect()
def drawRect():
x, y = pressed()
for i in range(1, taille // pas - 1):
for j in range(1, taille // pas - 1):
if i * pas <= x and x <= (i + 1) * pas:
if j * pas <= y and y <= (j + 1) * pas:
rect(i * pas, j * pas, pas, pas)
def pressed():
while True:
if mousePressed:
return (mouseX, mouseY)
taille=400
pas=taille//20
def设置():
尺寸(尾翼,尾翼)
背景(255、255、255)
笔划(0)
冲程重量(2)
帧率(20)
对于范围内的i(pas、taille、pas):
线(i、0、i、taille)
线(0,i,taille,i)
drawRect()
def drawRect():
x、 y=按下()
对于范围内的i(1,taille//pas-1):
对于范围内的j(1,尾部//pas-1):
如果我
因此,我希望出现一个网格,等待用户单击单元格,然后用黑色矩形填充该单元格。但是,我只想在代码的开头运行它,所以我没有把它放在draw函数中
无论如何,我建议使用draw
功能,根据程序的当前状态连续绘制场景
注意,您的程序挂起在一个无止境的循环中。变量mousePressed
、mouseX
和mouseY
永远不会更新。这些变量不会神奇地改变它们的状态。执行draw
函数后,它们在两帧之间更改状态。处理过程执行事件处理并更改内置变量。你没有给处理任何机会做这项工作
创建变量,注意“单击”的x和y窗口坐标:
执行鼠标按下事件以接收“单击”:
完整代码可能如下所示:
taille = 400
pas = taille // 20
def setup():
size(taille, taille)
background(255, 255, 255)
stroke(0)
strokeWeight(2)
frameRate(20)
for i in range(pas, taille, pas):
line(i, 0, i, taille)
line(0, i, taille, i)
enter_x = -1
enter_y = -1
def mousePressed():
global enter_x, enter_y
if enter_x < 0 or enter_y < 0:
enter_x = mouseX
enter_y = mouseY
def draw():
global enter_x, enter_y
if enter_x >= 0 and enter_y >= 0:
stroke(0)
fill(0)
ix = enter_x // pas
iy = enter_y // pas
rect(ix * pas, iy * pas, pas, pas)
taille=400
pas=taille//20
def设置():
尺寸(尾翼,尾翼)
背景(255、255、255)
笔划(0)
冲程重量(2)
帧率(20)
对于范围内的i(pas、taille、pas):
线(i、0、i、taille)
线(0,i,taille,i)
输入_x=-1
输入_y=-1
def mousePressed():
全局输入x,输入y
如果输入_x<0或输入_y<0:
输入_x=mouseX
输入_y=mouseY
def draw():
全局输入x,输入y
如果输入\u x>=0并输入\u y>=0:
笔划(0)
填充(0)
ix=输入\u x//pas
iy=输入_y//pas
rect(ix*pas,iy*pas,pas,pas)
注意,可能还需要在draw
函数中绘制网格。一般来说,与“撤消”已绘制的内容相比,最好在每一帧重新绘制场景
def mousePressed():
global enter_x, enter_y
if enter_x < 0 or enter_y < 0:
enter_x = mouseX
enter_y = mouseY
def draw():
global enter_x, enter_y
if enter_x >= 0 and enter_y >= 0:
stroke(0)
fill(0)
ix = enter_x // pas
iy = enter_y // pas
rect(ix * pas, iy * pas, pas, pas)
taille = 400
pas = taille // 20
def setup():
size(taille, taille)
background(255, 255, 255)
stroke(0)
strokeWeight(2)
frameRate(20)
for i in range(pas, taille, pas):
line(i, 0, i, taille)
line(0, i, taille, i)
enter_x = -1
enter_y = -1
def mousePressed():
global enter_x, enter_y
if enter_x < 0 or enter_y < 0:
enter_x = mouseX
enter_y = mouseY
def draw():
global enter_x, enter_y
if enter_x >= 0 and enter_y >= 0:
stroke(0)
fill(0)
ix = enter_x // pas
iy = enter_y // pas
rect(ix * pas, iy * pas, pas, pas)