我正在用Python创建一个基于用户输入创建形状的程序。我需要创建两个函数来使用Zeller's graphic.py文件创建按钮。一个按钮需要说退出,第二个按钮需要说进程。以下是到目前为止我所拥有的,但正如您所见,它们不在定义的函数中:
#create Quit button
quitButton = Text(Point(70,73), "Quit")
quitButton.draw(w)
Rectangle(Point(45, 50), Point(95,97)).draw(w)
#create Process button
enterButton = Text(Point(145,73), "Process")
enterButton.draw(w)
Rectangle(Point(120, 48), Point(170,98)).draw(w)
以下是必要方法的说明
createButton(text, pt1button, pt2button, win)
创建一个角点为pt1button
和{clickedButton(button, clickPt)
如果clickPt
在按钮中,则返回true/false。在我试图创建函数,但收到以下错误。在
我的职能是:
^{pr2}$这里是我调用函数的地方:
createButton("Process",145,73,win)
createButton("Quit",70,73,win)
以下是引发的错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/project4FINAL.py", line 77, in <module> main()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/project4FINAL.py", line 27, in main buttonCreate("Process", 145,73, win)
NameError: global name 'win' is not defined
有什么建议吗?在
因此,从代码来看,您似乎希望为每个按钮创建一个回调函数,然后通过
GraphWin
方法的setMouseHandler
将每个按钮分配给画布。在因此,从API中给出的示例来看:
除非在回调中进行了边界检查(以查看鼠标在画布上的哪个形状),否则每个绘制的形状应该只有一个画布。在
一个使用
^{pr2}$createButton
函数的示例:下面是使用新按钮对象的完整示例:
相关问题 更多 >
编程相关推荐