使用checkmouse

时间:2018-03-29 08:17:55

标签: python-3.x

我试图使用checkmouse从窗口中取出一些东西。当有人点击按钮时,它应该取消文本并写下其他内容。我使用checkMouse和getX()以及getY()来执行此操作,但我一直收到此错误,指出:

 File "C:\Users\User\Documents\python\project2.py", line 71, in panel
    if (clicknew.getX()>90 and clicknew.getX()<210) and (clicknew.getY()>35 and clicknew.getY() < 0):
AttributeError: 'NoneType' object has no attribute 'getX'

我到目前为止所做的这段代码如下:

from graphics import *

#creating the game panel window
def panel():
    #grey window, with coordinates flipped, with banners etc
    win = GraphWin("Start Panel", 300,200)
    win.setCoords(0,0,300,200)
    win.setBackground("light grey")
    #drawing the BoilerMazer banner with text
    boilermazer = Rectangle(Point(0,200),Point(300,160))
    boilermazer.setFill("white")
    boilermazer.draw(win)
    #text inside
    banner1 = Text(Point(150,180),"BoilerMazer")
    banner1.setStyle("bold")
    banner1.setSize(20)
    banner1.draw(win)
    #initial game panel is going to have two buttons and a top scores object
    #top score "screen"
    toprec = Rectangle(Point(60,140),Point(240,50))
    toprec.setFill("white")
    toprec.draw(win)
    #text inside toprec
    topscores = Text(Point(150,130),"TOP SCORES")
    topscores.setSize(8)
    topscores.draw(win)
    border = Text(Point(150,120),"======")
    border.draw(win)
    bigmac = Text(Point(150,110),"Big Mac     21")
    bigmac.setSize(8)
    bigmac.draw(win)
    tt = Text(Point(150,90),"T.T     23")
    tt.setSize(8)
    tt.draw(win)
    cshell = Text(Point(150,75),"C-Shell     25")
    cshell.setSize(8)
    cshell.draw(win)
    macmac = Text(Point(150,55),"MacMac     27")
    macmac.setSize(8)
    macmac.draw(win)
    #new player button that will eventually be clicked
    new1 = Point(90,35)
    new2 = Point(210,0)
    newrec = Rectangle(new1,new2)
    newrec.setFill("chartreuse2")
    newrec.draw(win)
    #new player button text
    newplayer = Text(Point(150,18),"NEW PLAYER")
    newplayer.draw(win)
    #reset button
    resetrec = Rectangle(Point(240,35),Point(300,0))
    resetrec.setFill("red")
    resetrec.draw(win)
    #resettext
    reset = Text(Point(270,18),"RESET")
    reset.draw(win)
    #secondary panel window is the game panel after they click new player
    #set up points that we check between for the new player button first
    #setting up the checkmouse
    clicknew = win.checkMouse()
    if (clicknew.getX()>90 and clicknew.getX()<210) and (clicknew.getY()>35 and clicknew.getY() < 0):
        newplayer.undraw()

您可以在此处找到图形窗口:http://mcsp.wartburg.edu/zelle/python/graphics.py

我不明白我做错了什么,是否还有其他一些我应该使用的方法?谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

根据docs,如果之前未检测到鼠标点击,则checkMouse()会返回None。似乎就是这种情况。

您可以围绕checkMouse的来电循环,并继续检查if clicknew is not None,只有在这种情况下继续执行您的计划。但也许有更好的方式......

更新:

示例:

while True:
     clicknew = win.getMouse()
     if clicknew:
         break
     else:
         time.sleep(0.1)  # avoid busy waiting           

# clicknew is set now => use it