简体   繁体   English

如何使用Zelle的graphics.py更改帧但仍然使用相同的窗口?

[英]How do you change frames but still use the same window using Zelle's graphics.py?

Im SO SO SO stuck on this AND becoming really desperate now.. WE have to create a mini program using the graphics.py library. 我也是如此坚持这一点并且现在变得非常绝望..我们必须使用graphics.py库创建一个迷你程序。 So basically what i want to know is how can I have like a start menu and then when the user clicks.. it changes to a different frame of the 'game' essentially?? 所以基本上我想知道的是我怎么能有一个开始菜单,然后当用户点击它时,它会改变为“游戏”的不同框架?

My friend suggested using the .undraw() function but i dont think that works for text & also just changing the .setText("") to an empty string as im trying to get rid of the title & subheading... This is my code: 我的朋友建议使用.undraw()函数,但我不认为这适用于文本,也只是将.setText("")更改为空字符串,因为我试图摆脱标题和副标题...这是我的码:

from graphics import *
# create the window to hold the contents
win = GraphWin("SHAPES FRENZY", 800, 500)
win.setBackground("Black")
#Setting the Title Page:
title=Text(Point(400,250),"Shapes Frenzy.")
title.draw(win)
#Subheading
subheading=Text(Point(400,290), "Click anywhere to continue.")
subheading.draw(win)

#Next Frame
clickPoint = win.getMouse()
if clickPoint == True:
    title.undraw() #THIS DOESNT WORK.
    subheading.undraw()

#Setting up Second frame of program.
hi=Text(Point(500,250),"Please Work.")
hi.setFace('courier')
hi.draw(win)

win.getMouse() # Pause to view result
win.close()    # Close window when done

don't worry - breath :) 别担心 - 口气:)

win.getMouse() isn't working as you think it is. win.getMouse()没有像你想象的那样工作。 what you want is really like this: 你想要的是这样的:

clickPoint = win.getMouse()
title.setText("")  #THIS DOESNT WORK.
subheading.setText("")

win.getMouse() will return a Point object. win.getMouse()将返回一个Point对象。 Here is a stripped definition from the library: 以下是库中的剥离定义:

def getMouse(self):
    """Wait for mouse click and return Point object representing
    the click"""
    # gets some stuff ready
    while self.mouseX == None or self.mouseY == None:
        # waits for click
    # post click code
    return Point(x,y)

The doc string really says it all.. but essentially win.getMouse() is going to block your program because it is going to be running inside that while loop until a click is registered. 文档字符串真的说明了一切..但实际上win.getMouse()将阻止你的程序,因为它将在while循环中运行,直到注册了一个点击。 Finally, notice that Point(x,y) is returned. 最后,注意返回Point(x,y)

What was going wrong was... 出了什么问题是......

You were assigning the output win.getMouse() of win.getMouse to a variable. 您将win.getMouse()的输出win.getMouse()分配给变量。 This variable's value would look something like this: Point(161.0, 247.0) . 这个变量的值看起来像这样: Point(161.0, 247.0) so when it hit your if expression. 所以当它击中你的if表达式时。 Python was checking that Point(161.0, 247.0) == True . Python正在检查Point(161.0, 247.0) == True Well, obviously it doesn't! 好吧,显然它没有! so, what was happening is you're if block was never getting executed, and the setText you tried was never run in the first place. 所以,发生的事情是你是否因为块永远不会被执行,而你尝试过的setText从未在第一时间运行。

For future debugging, try adding print statement to ensure code is executed when you think it is :). 对于将来的调试,请尝试添加print语句以确保在您认为代码执行时执行代码:)。 Eg 例如

print("this will definitely print")
if clickPoint == True:
    print("Code is being run!")
    title.undraw() #THIS DOESNT WORK.
    subheading.undraw()

And you'll notice that in the output of your program, the second print statement never occurred. 而且你会注意到在程序的输出中,第二个打印语句从未发生过。

Also, if you open up the graphics library (it is pretty small so not too big of a deal) and take a look at the Text object you'll see that there is no undraw method defined for the Text class. 此外,如果你打开图形库(它很小,所以不是太大的交易)并看看Text对象,你会看到没有为Text类定义的undraw方法。 there is a setText method available, so I went ahead an used that method and set the text to an empty string. 有一个setText方法可用,所以我继续使用该方法并将文本设置为空字符串。

Ah, last thing. 啊,最后一件事。 Since your goal of the if statement was to just hold your program until the user clicked anywhere, and the getMouse method did that anyways (stuck in while loop until a click occurs), the correct thing to do is to just drop the if statement entirely. 由于你的if语句的目标只是保持你的程序,直到用户点击任何地方,并且getMouse方法仍然这样做(陷入while循环直到发生点击),正确的做法是完全删除if语句。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM