Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将getMouse()函数应用于窗口的一部分_Python_Python 3.x_Zelle Graphics - Fatal编程技术网

Python 将getMouse()函数应用于窗口的一部分

Python 将getMouse()函数应用于窗口的一部分,python,python-3.x,zelle-graphics,Python,Python 3.x,Zelle Graphics,我试图将getMouse()函数应用于窗口的特定部分,而不是整个窗口。我需要的块,点击在改变颜色,如果它是'rect1'。但是,如果单击任何其他块,则不会发生任何事情。我已附上我的代码部分,我觉得与此有关的情况下,任何人都可以提供任何帮助 #draw the grid for j in range (6): for i in range(6): sq_i = Rectangle(Point(20 + (40*i), 20 + (40*j)),

我试图将getMouse()函数应用于窗口的特定部分,而不是整个窗口。我需要的块,点击在改变颜色,如果它是'rect1'。但是,如果单击任何其他块,则不会发生任何事情。我已附上我的代码部分,我觉得与此有关的情况下,任何人都可以提供任何帮助

#draw the grid 
for j in range (6):
    for i in range(6):
        sq_i = Rectangle(Point(20 + (40*i), 20 + (40*j)),
                         Point(60 + (40*i),60 + (40*j)))
        sq_i.draw(window)
        sq_i.setFill('white')
        sq_i.setOutline('grey')

#wait for a click 
window.getMouse ()

#turn the alotted region red
rect1 = Rectangle(Point(20 + (40*1), 20 + (40*1)),
                         Point(60 + (40*1), 60 + (40*1)))
rect1.setOutline('black')
rect1.draw(window)
rect1.setFill('brown')

#if the mouse is clicked in rect1, change the block color to black 
while window.getMouse() in rect1:
    rect1.setFill('black')

首先,您需要了解rect1中的
window.getMouse()的功能。Python的
in
操作符通过将b
中的
a转换为方法调用
b来工作。不幸的是,
Rectangle
类没有
\uuuuuuuuuuuuuuuu
方法。这是你眼前的问题

因此,您需要使用不同的测试。我建议您自己使用Python的链式比较运算符进行边界检查(在
图形
模块中似乎没有任何库支持):

mouse = window.getMouse()
if rect1.p1.x < mouse.x < rect1.p2.x and rect1.p1.y < mouse.y < rect1.p2.y
    rect1.setFill("black")
while True:
    mouse = window.getMouse()
    if rect1.p1.x < mouse.x < rect1.p2.x and rect1.p1.y < mouse.y < rect1.p2.y
        rect1.setFill("black")
        break