本文整理汇总了Python中timer.Timer.pause方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.pause方法的具体用法?Python Timer.pause怎么用?Python Timer.pause使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类timer.Timer
的用法示例。
在下文中一共展示了Timer.pause方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestTimer
# 需要导入模块: from timer import Timer [as 别名]
# 或者: from timer.Timer import pause [as 别名]
class TestTimer(unittest.TestCase):
def setUp(self):
self.timer = Timer()
def test_timer_is_running_if_it_is_started(self):
self.timer.start()
self.assertTrue(self.timer.running)
def test_timer_is_not_running_if_it_is_paused(self):
self.timer.pause()
self.assertFalse(self.timer.running)
def test_time_left_is_decremented_one_second_if_timer_is_running(self):
self.time_left = 300
self.timer.start()
self.timer.update()
self.assertEqual(self.timer.time_left, 299)
示例2: Preview
# 需要导入模块: from timer import Timer [as 别名]
# 或者: from timer.Timer import pause [as 别名]
class Preview(threading.Thread):
'''
Preview is a thread object that act as a pygame display control
PyGame issue on Windows (and possibly other OS than Linux):
In Windows OS, pygame just collects events in a main therad.
Then, the application must instatiate the Preview
and maint it's controler
'''
NONE = -1
STARTING = 0
RUNNING = 1
STOPPING = 2
KILLED = 3
preview_control = None
def __init__(self, title = "Grid visualizer",
width = PreviewDefaults.width,
height = PreviewDefaults.height,
lines = 2, columns = 2, fps_limit = 0,
painter_class = Painter):
# state control
self.state = Preview.STARTING
self.default_painter_class = painter_class
# thread control
self.threading = True
# Window app control
self.width = width
self.height = height
self.bgcolor = Color.BLACK
self.display = pygame.display.set_mode((width +
VerticalScroll.BUTTOM_SIZE,
height),
pygame.HWSURFACE |
pygame.DOUBLEBUF)
pygame.display.set_caption(title)
# diagram selector
lines = max([1, int(lines)])
columns = max([1, int(columns)])
self.grid = Grid(self.display, lines, columns)
self.mouse_drag = False
self.key_pressed = None
# timer control (to pause, FPS, keyboard)
self.fps_limit = fps_limit
self.frame_timer = Timer(0)
if self.fps_limit > 0:
self.frame_timer.set_delay(1.0 / self.fps_limit)
self.key_repeat_delay = 1.0 / 2.0
self.key_repeat_rate = 1.0 / 10.0
self.keyboard_timer = Timer(0)
# count number of painters/objects
self.count_painters = 0
# map object to painter
self.views = dict()
# multi-thread control
self.lock = threading.Lock()
def panel_dimensions(self):
'''
Get panel dimensions
'''
return self.grid.panel_width, self.grid.panel_height
def set_title(self, title):
'''
Change app window caption
'''
pygame.display.set_caption(title)
def add_view(self, view, label='', painter_class = None):
'''
Include a diagram in previewer
'''
assert view is not None
with self.lock:
# prevent drawing duplication of same object
if view in self.views:
return False
# create a painter to object
if painter_class is None:
painter = self.default_painter_class(self.count_painters,
view, label)
else:
painter = painter_class(self.count_painters, view, label)
self.count_painters += 1
# make control
self.grid.add_painter(painter)
self.views[view] = painter
return view
def del_view(self, view):
assert view is not None
with self.lock:
if view in self.views:
painter = self.views[view]
self.grid.del_painter(painter)
del self.views[view]
def pause(self, delay = 0.0):
#.........这里部分代码省略.........
示例3: EventRecorder
# 需要导入模块: from timer import Timer [as 别名]
# 或者: from timer.Timer import pause [as 别名]
class EventRecorder( QObject ):
"""
Records spontaneous events from the UI and serializes them as strings that can be evaluated in Python.
"""
def __init__(self, parent=None, ignore_parent_events=True):
QObject.__init__(self, parent=parent)
self._ignore_parent_events = False
if parent is not None and ignore_parent_events:
self._ignore_parent_events = True
self._parent_name = get_fully_qualified_name(parent)
self._captured_events = []
self._timer = Timer()
assert isinstance(QApplication.instance(), EventRecordingApp)
QApplication.instance().aboutToNotify.connect( self.handleApplicationEvent )
# We keep track of which mouse buttons are currently checked in this set.
# If we see a Release event for a button we think isn't pressed, then we missed something.
# In that case, we simply generate a fake Press event. (See below.)
# This fixes a problem that can occur in Linux:
# If the application doesn't have focus, but the user clicks a button in the window anyway,
# the window is activated and the button works normally during recording.
# However, the "press" event is not recorded, and the button is never activated during playback.
# This hack allows us to recognize that we missed the click and generate it anyway.
self._current_observed_mouse_presses = set()
def handleApplicationEvent(self, receiver, event):
if not self.paused:
self.captureEvent(receiver, event)
@property
def paused(self):
return self._timer.paused
IgnoredEventTypes = set( [ QEvent.Paint,
QEvent.KeyboardLayoutChange,
QEvent.WindowActivate,
QEvent.WindowDeactivate,
QEvent.ActivationChange,
QEvent.FileOpen,
QEvent.Clipboard,
# These event symbols are not exposed in pyqt, so we pull them from our own enum
EventTypes.Style,
EventTypes.ApplicationActivate,
EventTypes.ApplicationDeactivate,
EventTypes.NonClientAreaMouseMove,
EventTypes.NonClientAreaMouseButtonPress,
EventTypes.NonClientAreaMouseButtonRelease,
EventTypes.NonClientAreaMouseButtonDblClick
] )
IgnoredEventClasses = (QChildEvent, QTimerEvent, QGraphicsSceneMouseEvent, QWindowStateChangeEvent, QMoveEvent)
def captureEvent(self, watched, event):
if self._shouldSaveEvent(event):
try:
eventstr = event_to_string(event)
except KeyError:
logger.warn("Don't know how to record event: {}".format( str(event) ))
print "Don't know how to record", str(event)
else:
# Perform a full garbage collection before determining the name of this widget
gc.collect()
if sip.isdeleted(watched):
return
timestamp_in_seconds = self._timer.seconds()
objname = str(get_fully_qualified_name(watched))
if not ( self._ignore_parent_events and objname.startswith(self._parent_name) ):
# Special case: If this is a MouseRelease and we somehow missed the MousePress,
# then create a "synthetic" MousePress and insert it immediately before the release
if event.type() == QEvent.MouseButtonPress or event.type() == QEvent.MouseButtonDblClick:
self._current_observed_mouse_presses.add( event.button() )
elif event.type() == QEvent.MouseButtonRelease:
try:
self._current_observed_mouse_presses.remove( event.button() )
except KeyError:
synthetic_press_event = QMouseEvent( QEvent.MouseButtonPress, event.pos(), event.globalPos(), event.button(), event.buttons(), event.modifiers() )
synthetic_eventstr = event_to_string(synthetic_press_event)
self._captured_events.append( (synthetic_eventstr, objname, timestamp_in_seconds) )
self._captured_events.append( (eventstr, objname, timestamp_in_seconds) )
return
def insertComment(self, comment):
self._captured_events.append( (comment, "comment", None) )
def _shouldSaveEvent(self, event):
if isinstance(event, QMouseEvent):
# Ignore most mouse movement events if the user isn't pressing anything.
if event.type() == QEvent.MouseMove \
and int(event.button()) == 0 \
and int(event.buttons()) == 0 \
and int(event.modifiers()) == 0:
# If mouse tracking is enabled for this widget,
# then we'll assume mouse movements are important to it.
widgetUnderCursor = QApplication.instance().widgetAt( QCursor.pos() )
if widgetUnderCursor is not None and widgetUnderCursor.hasMouseTracking():
return True
# Somewhat hackish (and slow), but we have to record mouse movements during combo box usage.
# Same for QMenu usage (on Mac, it doesn't seem to matter, but on Fedora it does matter.)
#.........这里部分代码省略.........
示例4: PyTetrisModel
# 需要导入模块: from timer import Timer [as 别名]
# 或者: from timer.Timer import pause [as 别名]
class PyTetrisModel(object):
blockList = {"I": Block([[1, 1, 1, 1]]),
"J": Block([[1, 0, 0],
[1, 1, 1]]),
"L": Block([[0, 0, 1],
[1, 1, 1]]),
"O": Block([[1, 1],
[1, 1]]),
"S": Block([[0, 1, 1],
[1, 1, 0]]),
"T": Block([[0, 1, 0],
[1, 1, 1]]),
"Z": Block([[1, 1, 0],
[0, 1, 1]])}
blockColorList = [(0, 0, 0),
(194, 54, 33),
(37, 188, 36),
(173, 173, 39),
(73, 46, 255),
(211, 56, 211),
(51, 187, 200),
(203, 204, 205)]
def __init__(self):
self._state = "initialized"
self._difficulty = 'crazy'
# map 以左上角为原点,从左往右是 x 正方向,从上往下是 y 正方向。
#
# (0, 0) (1, 0) (2, 0) ... (9, 0)
# (0, 1) (1, 1) (2, 1) ... (9, 1)
# ... ... ... ... ...
# (0, 19) (1, 19) (2, 19) ... (9, 19)
#
self.map = Block([[None]*10 for i in range(20)])
self.timer = None
self.next_block = Block([[]])
self.active_block = Block([[]])
self.active_block_position = (0, 0)
self.gameover_callback = None
# 在按左右方向键时,可能会和正在对自身进行访问的 tick 函数造成的冲突。
# 所以这儿准备一个 Lock 。
self.lock = Lock()
self.score = 0
@property
def difficulty(self):
return self._difficulty
@difficulty.setter
def difficulty(self, value):
if self.state == "initialized":
if value in DIFFICULTY.keys():
self.difficulty = value
else:
raise NoSuchDifficulty
else:
raise WrongState
@property
def state(self):
return self._state
@state.setter
def state(self, value):
stderr.write("model: set state to " + str(value) + "\n")
self._state = value
def start(self):
if self.state == "initialized":
self.state = "start"
self.timer = Timer(target=self.tick,
interval=DIFFICULTY[self.difficulty]['interval'])
self.timer.run()
else:
stderr.write("model: the state is not initialized, can not start the game")
def pause(self):
self.timer.pause()
def resume(self):
self.timer.resume()
def press_arrow_key(self, direction):
if self.state == "falling":
with self.lock:
#.........这里部分代码省略.........