2014-09-12 66 views
0

在我的程序中,我尝试随机播放一些声音。但问题是,我听不到他们。我没有得到任何错误或类似的东西,我只是听不到声音。谢谢您的帮助。对不起,如果我的英语不好。而且,这里是我的代码:Python模块wx不播放音频声音

import Tkinter as tk 
import random 
import wx 

class SampleApp(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     self.state("zoomed") 

     container= tk.Frame(self) 
     container.config() 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=2) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames={} 
     for F in (fenster, fensta): 
      frame= F(container, self) 
      self.frames[F]=frame 
      frame.grid(row=0, column=0, sticky="nsew") 
      frame.pack_propagate(0) 

     self.show_frame(fenster) 

    def show_frame(self, c): 
     frame=self.frames[c] 
     frame.tkraise() 

class fenster(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 

     button=tk.Button(self, text="Start", 
         command=lambda: controller.show_frame(fensta)) 
     button.pack() 

class fensta(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     def ok(): 
      def do_a(): 
       def __init__(self, parent): 
        wx.Frame.__init__(self, parent) 
        self.test = wx.Sound('ja.wav') 
        self.test.Play(wx.SOUND_SYNC) 

       print("a") 

      def do_b(): 
       def __init__(self, parent): 
        wx.Frame.__init__(self, parent) 
        self.testa = wx.Sound('nein.wav') 
        self.testa.Play(wx.SOUND_SYNC) 

       print("b") 

      a=random.randint(1,2) 

      if a==1: 
       do_a() 
      elif a==2: 
       do_b() 

     button=tk.Button(self, text="press", command=ok) 
     button.pack() 

if __name__== "__main__": 
    app=SampleApp() 
    app.mainloop() 
+0

你在使用什么平台? – 2014-09-12 17:10:38

+0

@DizietAsahi我正在使用python IDLE 2.7 – Duardo 2014-09-12 17:36:47

+0

Windows? OSX? Linux呢?你是如何安装wxpython的? – 2014-09-12 20:25:31

回答

0

来测试你是否有wxPython的本身有问题,使用应该已经安装在系统上的wxPython the sound sample code

如果您无法获得示例代码的声音,那么您的wxpython有问题。在某些linux发行版上有一些问题,并且如果声音示例工作得很好,那么你的问题可能是由于你在同一个程序中混合了tk和wx。这是有原因的吗?

+0

我认为你是对的。问题似乎是我混合tk和wx。当我尝试仅在wx中播放声音时才起作用,但是当我尝试使用Tkinter构建GUI时,它不起作用。无论如何,感谢您的帮助。 – Duardo 2014-09-13 20:09:47

0

你不能混合wxPython和Tkinter,句号。