python满分大作业打地鼠,用graphics库编写,内含文档。
代码片段和文件信息
# button.py
from graphics import *
class Button:
“““A button is a labeled rectangle in a window.
It is activated or deactivated with the activate()
and deactivate() methods. The clicked(p) method
returns true if the button is active and p is inside it.“““
def __init__(self win center width height label):
“““ Creates a rectangular button eg:
qb = Button(myWin Point(3025) 20 10 ‘Quit‘) “““
wh = width/2.0 height/2.0
xy = center.getX() center.getY()
self.xmax self.xmin = x+w x-w
self.ymax self.ymin = y+h y-h
p1 = Point(self.xmin self.ymin)
p2 = Point(self.xmax self.ymax)
self.rect = Rectangle(p1p2)
self.rect.setFill(‘lightgray‘)
self.rect.draw(win)
self.label = Text(center label)
self.label.draw(win)
self.deactivate()
def clicked(self p):
“RETURNS true if button active and p is inside“
return self.active and
self.xmin <= p.getX() <= self.xmax and
self.ymin <= p.getY() <= self.ymax
def getLabel(self):
“RETURNS the label string of this button.“
return self.label.getText()
def activate(self):
“Sets this button to ‘active‘.“
self.label.setFill(‘black‘)
self.rect.setWidth(2)
self.active = 1
def deactivate(self):
“Sets this button to ‘inactive‘.“
self.label.setFill(‘slategrey‘)
self.rect.setWidth(1)
self.active = 0
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)