<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Townes &#187; python alarm</title>
	<atom:link href="https://thetownes.coolpage.biz/?feed=rss2&#038;tag=python-alarm" rel="self" type="application/rss+xml" />
	<link>https://thetownes.coolpage.biz</link>
	<description>本站已转移至：http://www.thetownes.info</description>
	<lastBuildDate>Thu, 01 Aug 2013 11:49:56 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.2</generator>
		<item>
		<title>python alarm</title>
		<link>https://thetownes.coolpage.biz/?p=136</link>
		<comments>https://thetownes.coolpage.biz/?p=136#comments</comments>
		<pubDate>Tue, 09 Jul 2013 15:00:35 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[python alarm]]></category>

		<guid isPermaLink="false">http://thetownes.coolpage.biz/?p=136</guid>
		<description><![CDATA[import time import sys soundFile = ‘soun &#8230; <a href="https://thetownes.coolpage.biz/?p=136">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><code>import time<br />
import sys</code></p>
<p>soundFile = ‘sound.wav’<br />
not_executed = 1</p>
<p>def soundStart():<br />
if sys.platform[:5] == ‘linux’:<br />
import os<br />
os.popen2(‘aplay -q’ + soundFile)<br />
else:<br />
import winsound<br />
winsound.PlaySound(soundFile, winsound.SND_FILENAME)</p>
<p>while(not_executed):<br />
dt = list(time.localtime())<br />
hour = dt[3]<br />
minute = dt[4]<br />
if hour == 17 and minute == 38: # 下午5点33分的时候开始提示<br />
soundStart()<br />
not_executed = 0<br />
winsound 模块提供访问由 Windows 平台提供的基本的声音播放设备。它包含函数和数个常量。</p>
<p>Beep(frequency, duration)<br />
蜂鸣PC的喇叭。 frequency 参数指定声音的频率，以赫兹，并且必须是在 37 到 32,767<br />
的范围之中。duration 参数指定声音应该持续的毫秒数。如果系统不能蜂鸣喇叭，挂起 RuntimeError。注意：Windows 95 和 98下，Windows Beep() 函数存在但是无效的(它忽略它的参数)。这种情况下Python通过直接的端口操作模拟它(2.1版本中增加的)。不知道是否在所有的系统上都工作。 1.6版本中的新特性。</p>
<p>PlaySound(sound, flags)<br />
从平台 API 中调用 PlaySound() 函数。sound 参数必须是一个文件名，音频数据作为字符串，或为 None。它的解释依赖于 flags 的值，该值可以是一个位方式或下面描述的变量的组合。如果系统显示一个错误，挂起 RuntimeError 。</p>
<p>MessageBeep([type=MB_OK])<br />
从平台 API 中调用 MessageBeep() 函数。播放一个在注册表中指定的声音。type 参数指定播放哪一个声音；可能的值是 -1，MB_ICONASTERISK，MB_ICONEXCLAMATION，MB_ICONHAND，MB_ICONQUESTION，和 MB_OK，所有的描述如下。值 -1 产生一个“简单的蜂鸣”；换句话说这是如果声音不能被播放的后备计划。2.3版本中的新特性。</p>
<p>SND_FILENAME<br />
sound 参数是一个 WAV 文件的名称。不使用 SND_ALIAS。</p>
<p>SND_ALIAS<br />
sound 参数是注册表中一个声音组合的名称。如果注册表没有包含这样的名称，播放系统缺省的声音除非 SND_NODEFAULT 也被指定。如果没有缺省的声音被注册，挂起 RuntimeError。不使用 SND_FILENAME。<br />
所有的 Win32 系统至少支持下列，大多数系统支持的更多：<br />
PlaySound() 名称 对应的控制面板声音名称<br />
‘SystemAsterisk’ Asterisk<br />
‘SystemExclamation’ Exclamation<br />
‘SystemExit’ Exit Windows<br />
‘SystemHand’ Critical Stop<br />
‘SystemQuestion’ Question<br />
例子：<br />
<code><br />
import winsound</code></p>
<p># Play Windows exit sound.<br />
winsound.PlaySound(“SystemExit”, winsound.SND_ALIAS)</p>
<p># Probably play Windows default sound, if any is registered (because<br />
# “*” probably isn’t the registered name of any sound).<br />
winsound.PlaySound(“*”, winsound.SND_ALIAS)</p>
<p>SND_LOOP<br />
重复地播放声音。SND_ASYNC标识也必须被用来避免堵塞。不能用 SND_MEMORY。</p>
<p>SND_MEMORY<br />
提供给PlaySound()的 sound 参数是一个 WAV 文件的内存映像(memory image)，作为一个字符串。<br />
注意：这个模块不支持从内存映像中异步播放，因此这个标识和 SND_ASYNC 的组合将挂起 RuntimeError。</p>
<p>SND_PURGE<br />
停止播放所有指定声音的实例。</p>
<p>SND_ASYNC<br />
立即返回，允许声音异步播放。</p>
<p>SND_NODEFAULT<br />
不过指定的声音没有找到，不播放系统缺省的声音。</p>
<p>SND_NOSTOP<br />
不中断当前播放的声音。</p>
<p>SND_NOWAIT<br />
如果声音驱动忙立即返回。</p>
<p>MB_ICONASTERISK<br />
播放 SystemDefault 声音。</p>
<p>MB_ICONEXCLAMATION<br />
播放 SystemExclamation 声音。</p>
<p>MB_ICONHAND<br />
播放 SystemHand 声音。</p>
<p>MB_ICONQUESTION<br />
播放 SystemQuestion 声音。</p>
<p>MB_OK<br />
播放 SystemDefault 声音。</p>
<p>实例一</p>
<p><code><br />
import wx<br />
from wx.lib.filebrowsebutton import FileBrowseButton</code></p>
<p>class MyFrame(wx.Frame):<br />
def __init__(self):<br />
wx.Frame.__init__(self, None, title=”wx.Sound”,size=(500,100))<br />
p = wx.Panel(self)</p>
<p>self.fbb = FileBrowseButton(p,labelText=”Select WAV file:”,fileMask=”*.wav”)<br />
btn = wx.Button(p, -1, “Play”)<br />
self.Bind(wx.EVT_BUTTON, self.OnPlaySound, btn)</p>
<p>sizer = wx.BoxSizer(wx.HORIZONTAL)<br />
sizer.Add(self.fbb, 1, wx.ALIGN_CENTER_VERTICAL)<br />
sizer.Add(btn, 0, wx.ALIGN_CENTER_VERTICAL)<br />
border = wx.BoxSizer(wx.VERTICAL)<br />
border.Add(sizer, 0, wx.EXPAND|wx.ALL, 15)<br />
p.SetSizer(border)</p>
<p>def OnPlaySound(self, evt):<br />
filename = self.fbb.GetValue()<br />
self.sound = wx.Sound(filename)<br />
if self.sound.IsOk():<br />
self.sound.Play(wx.SOUND_ASYNC)<br />
else:<br />
wx.MessageBox(“Invalid sound file”, “Error”)</p>
<p>app = wx.PySimpleApp()<br />
frm = MyFrame()<br />
frm.Show()<br />
app.MainLoop()</p>
<p>实例二<br />
<code><br />
import wx<br />
import wx.media<br />
import os</code></p>
<p>class Panel1(wx.Panel):<br />
def __init__(self, parent, id):<br />
#self.log = log<br />
wx.Panel.__init__(self, parent, -1, style=wx.TAB_TRAVERSAL|wx.CLIP_CHILDREN)</p>
<p># Create some controls<br />
try:<br />
self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)<br />
except NotImplementedError:<br />
self.Destroy()<br />
raise</p>
<p>loadButton = wx.Button(self, -1, “Load File”)<br />
self.Bind(wx.EVT_BUTTON, self.onLoadFile, loadButton)</p>
<p>playButton = wx.Button(self, -1, “Play”)<br />
self.Bind(wx.EVT_BUTTON, self.onPlay, playButton)</p>
<p>pauseButton = wx.Button(self, -1, “Pause”)<br />
self.Bind(wx.EVT_BUTTON, self.onPause, pauseButton)</p>
<p>stopButton = wx.Button(self, -1, “Stop”)<br />
self.Bind(wx.EVT_BUTTON, self.onStop, stopButton)</p>
<p>slider = wx.Slider(self, -1, 0, 0, 0, size=wx.Size(300, -1))<br />
self.slider = slider<br />
self.Bind(wx.EVT_SLIDER, self.onSeek, slider)</p>
<p>self.st_file = wx.StaticText(self, -1, “.mid .mp3 .wav .au .avi .mpg”, size=(200,-1))<br />
self.st_size = wx.StaticText(self, -1, size=(100,-1))<br />
self.st_len = wx.StaticText(self, -1, size=(100,-1))<br />
self.st_pos = wx.StaticText(self, -1, size=(100,-1))</p>
<p># setup the button/label layout using a sizer<br />
sizer = wx.GridBagSizer(5,5)<br />
sizer.Add(loadButton, (1,1))<br />
sizer.Add(playButton, (2,1))<br />
sizer.Add(pauseButton, (3,1))<br />
sizer.Add(stopButton, (4,1))<br />
sizer.Add(self.st_file, (1, 2))<br />
sizer.Add(self.st_size, (2, 2))<br />
sizer.Add(self.st_len, (3, 2))<br />
sizer.Add(self.st_pos, (4, 2))<br />
sizer.Add(self.mc, (5,1), span=(5,1)) # for .avi .mpg video files<br />
self.SetSizer(sizer)</p>
<p>self.timer = wx.Timer(self)<br />
self.Bind(wx.EVT_TIMER, self.onTimer)<br />
self.timer.Start(100)</p>
<p>def onLoadFile(self, evt):<br />
dlg = wx.FileDialog(self, message=”Choose a media file”,<br />
defaultDir=os.getcwd(), defaultFile=”&#8221;,<br />
style=wx.OPEN | wx.CHANGE_DIR )<br />
if dlg.ShowModal() == wx.ID_OK:<br />
path = dlg.GetPath()<br />
self.doLoadFile(path)<br />
dlg.Destroy()</p>
<p>def doLoadFile(self, path):<br />
if not self.mc.Load(path):<br />
wx.MessageBox(“Unable to load %s: Unsupported format?” % path, “ERROR”, wx.ICON_ERROR | wx.OK)<br />
else:<br />
folder, filename = os.path.split(path)<br />
self.st_file.SetLabel(‘%s’ % filename)<br />
self.mc.SetBestFittingSize()<br />
self.GetSizer().Layout()<br />
self.slider.SetRange(0, self.mc.Length())<br />
self.mc.Play()</p>
<p>def onPlay(self, evt):<br />
self.mc.Play()</p>
<p>def onPause(self, evt):<br />
self.mc.Pause()</p>
<p>def onStop(self, evt):<br />
self.mc.Stop()</p>
<p>def onSeek(self, evt):<br />
offset = self.slider.GetValue()<br />
self.mc.Seek(offset)</p>
<p>def onTimer(self, evt):<br />
offset = self.mc.Tell()<br />
self.slider.SetValue(offset)<br />
self.st_size.SetLabel(‘size: %s ms’ % self.mc.Length())<br />
self.st_len.SetLabel(‘( %d seconds )’ % (self.mc.Length()/1000))<br />
self.st_pos.SetLabel(‘position: %d ms’ % offset)</p>
<p>app = wx.PySimpleApp()<br />
# create a window/frame, no parent, -1 is default ID<br />
frame = wx.Frame(None, -1, “play audio and video files”, size = (320, 350))<br />
# call the derived class<br />
Panel1(frame, -1)<br />
frame.Show(1)<br />
app.MainLoop()</p>
]]></content:encoded>
			<wfw:commentRss>https://thetownes.coolpage.biz/?feed=rss2&#038;p=136</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
