几乎没有问题:
你不执行
main()
运行程序,但也许你没有添加这个只是个问题。
win
是局部变量,函数不能访问它们。您应该在错误消息中看到它为
NameError: name 'win' is not defined
在里面
rectDice.draw(win)
.
Traceback (most recent call last):
File "<pyshell#2>", line 30, in <module>
main()
File "<pyshell#2>", line 28, in main
dice("a","b")
File "<pyshell#2>", line 21, in dice
rectDice.draw(win)
NameError: name 'win' is not defined
您可以添加
赢
作为论据
def dice(win, a, b):
#build the dice
rectDice = Rectangle(Point(115, 115), Point(275,275))
rectDice.setFill('pink')
rectDice.setOutline('pink')
rectDice.draw(win)
def main():
# Create window, call dice function
win = GraphWin("Dice", 400, 400)
win.setBackground("cyan")
dice(win, "a","b")
或者必须将窗口分配给全局变量-使用
global
在里面
主体()
def dice(a, b):
#build the dice
rectDice = Rectangle(Point(115, 115), Point(275,275))
rectDice.setFill('pink')
rectDice.setOutline('pink')
rectDice.draw(win)
def main():
global win
# Create window, call dice function
win = GraphWin("Dice", 400, 400)
win.setBackground("cyan")
dice("a", "b")
同样的问题
dot()
在
点()
你还有其他问题。你必须使用
setFill()
在下一行
circDot1 = Circle(Point(50, 50), 5)
circDot1.setFill('black')
circDot1.draw(win)
完整代码:
from graphics import *
def dot(win, c, d):
#draw each dot
circDot1 = Circle(Point(50, 50), 5)
circDot1.setFill('black')
circDot1.draw(win)
circDot3 = Circle(Point(50, 100), 5)
circDot3.setFill('black')
circDot3.draw(win)
circDot5 = Circle(Point(100, 50), 5)
circDot5.setFill('black')
circDot5.draw(win)
circDot7 = Circle(Point(100, 100), 5)
circDot7.setFill('black')
circDot7.draw(win)
circDot4 = Circle(Point(75, 75), 5)
circDot4.setFill('black')
circDot4.draw(win)
def dice(win, a, b):
#build the dice
rectDice = Rectangle(Point(115, 115), Point(275,275))
rectDice.setFill('pink')
rectDice.setOutline('pink')
rectDice.draw(win)
def main():
# Create window, call dice function
win = GraphWin("Dice", 400, 400)
win.setBackground("cyan")
dice(win, "a", "b")
dot(win, "c", "d")
main()