简体   繁体   English

如何调用def(main)中的def(dot)和def(dice)函数?

[英]How do I call the functions: def(dot) and def(dice) from def(main)?

I'm having trouble calling the top two functions from the third(main) function. 我在从third(main)函数调用前两个函数时遇到麻烦。 I believe I have the coding correct(ignore the sizes and x,y coordinates) to display a the five side of a di but can't seem to figure out how to call the functions. 我相信我有正确的编码(忽略大小和x,y坐标)来显示di的五个面,但似乎无法弄清楚如何调用函数。

main objective is to display the five side of the di. 主要目的是展示di的五个面。

def dot(c,d):
 #draw each dot 
 circDot1 = Circle(Point(50, 50), 5).setFill('black')
 circDot1.draw(win)
 circDot3 = Circle(Point(50, 100), 5).setFill('black')
 circDot3.draw(win)
 circDot5 = Circle(Point(100, 50), 5).setFill('black')
 circDot5.draw(win)
 circDot7 = Circle(Point(100, 100), 5).setFill('black')
 circDot7.draw(win)
 circDot4 = Circle(Point(75, 75), 5).setFill('black')
 circDot4.draw(win)

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():
 # Create window, call dice function
 win = GraphWin("Dice", 400, 400)
 win.setBackground("cyan")
 dice("a","b")

There are few problems: 有几个问题:

You don't execute main() to run program but maybe you didn't add this only in question. 您无需执行main()即可运行程序,但也许您不是仅在有问题的地方添加了此代码。


win is local variable and functions don't have access to them. win是局部变量,函数无法访问它们。 You should see it in error message as NameError: name 'win' is not defined in rectDice.draw(win) . 您应该在错误消息中看到它为NameError: name 'win' is not definedrectDice.draw(win)rectDice.draw(win) NameError: name 'win' is not defined

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

You may add win as argument 您可以添加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")

or you would have to assign window to global variable - using global in main() 否则您必须将窗口分配给全局变量-在main()使用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")

The same problem is in dot() 同样的问题在dot()


In dot() you have other problem. dot()您还有其他问题。 You have to use setFill() in next line 您必须在下一行中使用setFill()

circDot1 = Circle(Point(50, 50), 5)
circDot1.setFill('black')
circDot1.draw(win)

Full code: 完整代码:

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() 

I think your main() function is not even called too. 我认为您的main()函数甚至都没有被调用。

try to use this: 尝试使用此:

def main():
 # Create window, call dice function
 win = GraphWin("Dice", 400, 400)
 win.setBackground("cyan")
 dice("a","b")

if __name__== "__main__":
 main()

Reference: this link 参考: 此链接

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

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