wxPython and Autobahn websockets - python

Im trying to create GUI app for my test project, based on Python/Twisted/Autobahn.ws. Im following article as like there: wxPython and Twisted and try to connect my application to the server. But at the start i'll get an error:
Unhandled Error
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/twisted/python/log.py", line 88, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/local/lib/python2.7/dist-packages/twisted/python/log.py", line 73, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/local/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/local/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/_threadedselect.py", line 283, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 593, in doConnect
self._connectDone()
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 607, in _connectDone
self.protocol = self.connector.buildProtocol(self.getPeer())
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/base.py", line 1071, in buildProtocol
return self.factory.buildProtocol(addr)
File "/usr/local/lib/python2.7/dist-packages/twisted/protocols/policies.py", line 171, in buildProtocol
return self.protocol(self, self.wrappedFactory.buildProtocol(addr))
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/protocol.py", line 123, in buildProtocol
p = self.protocol()
exceptions.AttributeError: GUIClientProtocol instance has no __call__ method
How i can fix this issue? Need to define call method in my class or maybe i have better solution for this?
Code:
1) client_gui:
import wx
from twisted.internet import wxreactor
wxreactor.install()
from twisted.internet import reactor, ssl
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS
from gui.filemanager import CloudStorage
class GUIClientProtocol(WebSocketClientProtocol):
def __init__(self, gui):
self.gui = gui
if __name__ == '__main__':
app = wx.App(False)
frame = CloudStorage(None, -1, 'CloudStorage')
frame.Show()
host_url = "wss://%s:%s" % ("localhost", 9000)
# create a WS server factory with our protocol
factory = WebSocketClientFactory(host_url, debug = False)
factory.protocol = GUIClientProtocol(frame)
# SSL client context: using default
if factory.isSecure:
contextFactory = ssl.ClientContextFactory()
else:
contextFactory = None
reactor.registerWxApp(app)
connectWS(factory, contextFactory)
reactor.run()
2) server.py
import sys
import datetime
import pickle
from json import dumps, loads
from hashlib import sha256
from time import gmtime, strftime
from subprocess import Popen, PIPE, STDOUT
import sqlalchemy
import sqlalchemy.exc
from sqlalchemy import and_, func, asc
from sqlalchemy.orm import sessionmaker
from twisted.internet import reactor, ssl
from twisted.internet.task import deferLater
from twisted.python import log, logfile
from twisted.web.server import Site
from twisted.web.wsgi import WSGIResource
from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol, listenWS
from balancer.balancer import Balancer
from db.tables import File as FileTable
from db.tables import Users, FileServer, FileSpace, Catalog
from flask_app import app
from utils import commands
log_file = logfile.LogFile("service.log", ".")
log.startLogging(log_file)
engine = sqlalchemy.create_engine('postgresql://user:password#localhost/csan', pool_size=20, max_overflow=0)
def checkServerStatus(ip, port):
p = Popen(["python", "statuschecker.py", str(ip), str(port)], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
result = p.communicate()[0].replace('\n', '')
return result.split('|')
class DFSServerProtocol(WebSocketServerProtocol):
... # ~450 lines of code
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
log.startLogging(sys.stdout)
debug = True
port = int(sys.argv[2])
else:
debug = False
port = int(sys.argv[1])
contextFactory = ssl.DefaultOpenSSLContextFactory('web/keys/server.key', 'web/keys/server.crt')
server_addr = "wss://localhost:%d" % (port)
factory = WebSocketServerFactory(server_addr, debug = debug, debugCodePaths = debug)
factory.protocol = DFSServerProtocol
factory.setProtocolOptions(allowHixie76 = True)
listenWS(factory, contextFactory)
# Flask with SSL under Twisted
resource = WSGIResource(reactor, reactor.getThreadPool(), app)
site = Site(resource)
reactor.listenSSL(8080, site, contextFactory)
# reactor.listenTCP(8080, web)
reactor.run()
Update #1:
client_gui.py looks like that:
import wx
from twisted.internet import wxreactor
wxreactor.install()
from twisted.internet import reactor, ssl
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS
from gui.filemanager import CloudStorage
class GUIClientProtocol(WebSocketClientProtocol):
def __init__(self):
WebSocketClientProtocol.__init__(self)
self.gui = None
def connectionMade(self):
self.gui = self.factory._frame
self.gui.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = CloudStorage(None, -1, 'CloudStorage')
# create a WS server factory with our protocol
host_url = "wss://%s:%s" % ("localhost", 9000)
factory = WebSocketClientFactory(host_url)
factory.protocol = GUIClientProtocol
factory._frame = frame
# SSL client context: using default
if factory.isSecure:
contextFactory = ssl.ClientContextFactory()
else:
contextFactory = None
reactor.registerWxApp(app)
connectWS(factory, contextFactory)
reactor.run()
Update #2:
code of my CloudStorage class:
import wx
import os
import time
ID_BUTTON=100
ID_EXIT=200
ID_SPLITTER=300
class MyListCtrl(wx.ListCtrl):
def __init__(self, parent, id):
wx.ListCtrl.__init__(self, parent, id, style=wx.LC_REPORT)
files = os.listdir('.')
images = ['images/empty.png', 'images/folder.png', 'images/source_py.png',
'images/image.png', 'images/pdf.png', 'images/up16.png']
self.InsertColumn(0, 'Name')
self.InsertColumn(1, 'Ext')
self.InsertColumn(2, 'Size', wx.LIST_FORMAT_RIGHT)
self.InsertColumn(3, 'Modified')
self.SetColumnWidth(0, 220)
self.SetColumnWidth(1, 70)
self.SetColumnWidth(2, 100)
self.SetColumnWidth(3, 420)
self.il = wx.ImageList(16, 16)
for i in images:
self.il.Add(wx.Bitmap(i))
self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
j = 1
self.InsertStringItem(0, '..')
self.SetItemImage(0, 5)
for i in files:
(name, ext) = os.path.splitext(i)
ex = ext[1:]
size = os.path.getsize(i)
sec = os.path.getmtime(i)
self.InsertStringItem(j, name)
self.SetStringItem(j, 1, ex)
self.SetStringItem(j, 2, str(size) + ' B')
self.SetStringItem(j, 3, time.strftime('%Y-%m-%d %H:%M',
time.localtime(sec)))
# if os.path.isdir(i):
# self.SetItemImage(j, 1)
# elif ex == 'py':
# self.SetItemImage(j, 2)
# elif ex == 'jpg':
# self.SetItemImage(j, 3)
# elif ex == 'pdf':
# self.SetItemImage(j, 4)
# else:
# self.SetItemImage(j, 0)
if (j % 2) == 0:
self.SetItemBackgroundColour(j, '#e6f1f5')
j = j + 1
class CloudStorage(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, -1, title)
self.splitter = wx.SplitterWindow(self, ID_SPLITTER, style=wx.SP_BORDER)
self.splitter.SetMinimumPaneSize(50)
p1 = MyListCtrl(self.splitter, -1)
p2 = MyListCtrl(self.splitter, -1)
self.splitter.SplitVertically(p1, p2)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_SPLITTER_DCLICK, self.OnDoubleClick, id=ID_SPLITTER)
filemenu= wx.Menu()
filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
editmenu = wx.Menu()
netmenu = wx.Menu()
showmenu = wx.Menu()
configmenu = wx.Menu()
helpmenu = wx.Menu()
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File")
menuBar.Append(editmenu, "&Edit")
menuBar.Append(netmenu, "&Net")
menuBar.Append(showmenu, "&Show")
menuBar.Append(configmenu, "&Config")
menuBar.Append(helpmenu, "&Help")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
tb = self.CreateToolBar( wx.TB_HORIZONTAL | wx.NO_BORDER |
wx.TB_FLAT | wx.TB_TEXT)
# tb.AddSimpleTool(10, wx.Bitmap('images/previous.png'), 'Previous')
# tb.AddSimpleTool(20, wx.Bitmap('images/up.png'), 'Up one directory')
# tb.AddSimpleTool(30, wx.Bitmap('images/home.png'), 'Home')
# tb.AddSimpleTool(40, wx.Bitmap('images/refresh.png'), 'Refresh')
# tb.AddSeparator()
# tb.AddSimpleTool(50, wx.Bitmap('images/write.png'), 'Editor')
# tb.AddSimpleTool(60, wx.Bitmap('images/terminal.png'), 'Terminal')
# tb.AddSeparator()
# tb.AddSimpleTool(70, wx.Bitmap('images/help.png'), 'Help')
tb.Realize()
self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
button1 = wx.Button(self, ID_BUTTON + 1, "F3 View")
button2 = wx.Button(self, ID_BUTTON + 2, "F4 Edit")
button3 = wx.Button(self, ID_BUTTON + 3, "F5 Copy")
button4 = wx.Button(self, ID_BUTTON + 4, "F6 Move")
button5 = wx.Button(self, ID_BUTTON + 5, "F7 Mkdir")
button6 = wx.Button(self, ID_BUTTON + 6, "F8 Delete")
button7 = wx.Button(self, ID_BUTTON + 7, "F9 Rename")
button8 = wx.Button(self, ID_EXIT, "F10 Quit")
self.sizer2.Add(button1, 1, wx.EXPAND)
self.sizer2.Add(button2, 1, wx.EXPAND)
self.sizer2.Add(button3, 1, wx.EXPAND)
self.sizer2.Add(button4, 1, wx.EXPAND)
self.sizer2.Add(button5, 1, wx.EXPAND)
self.sizer2.Add(button6, 1, wx.EXPAND)
self.sizer2.Add(button7, 1, wx.EXPAND)
self.sizer2.Add(button8, 1, wx.EXPAND)
self.Bind(wx.EVT_BUTTON, self.OnExit, id=ID_EXIT)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.splitter,1,wx.EXPAND)
self.sizer.Add(self.sizer2,0,wx.EXPAND)
self.SetSizer(self.sizer)
size = wx.DisplaySize()
self.SetSize(size)
self.sb = self.CreateStatusBar()
self.sb.SetStatusText(os.getcwd())
self.Center()
self.Show(True)
def OnExit(self,e):
self.Close(True)
def OnSize(self, event):
size = self.GetSize()
self.splitter.SetSashPosition(size.x / 2)
self.sb.SetStatusText(os.getcwd())
event.Skip()
def OnDoubleClick(self, event):
size = self.GetSize()
self.splitter.SetSashPosition(size.x / 2)
if __name__ == '__main__':
app = wx.App(0)
CloudStorage(None, -1, 'CloudStorage')
app.MainLoop()

Update: I have created a complete example of using wxPython with Autobahn to create WebSocket enabled UIs.
The problem is the line
factory.protocol = GUIClientProtocol(frame)
This sets factory.protocol to an instance of GUIClientProtocol. But it needs to be the class.
Now, you apparently want to have frame accessible from within GUIClientProtocol. There are (at least) two options.
Option 1:
factory.protocol = GUIClientProtocol
factory._frame = frame
and then access as self.factory._frame from the (running) protocol instance.
Option 2: Implement Factory.buildProtocol.

Related

wxPython : Adding an about box.

I'm creating a wxPython soundboard and was wondering how to implement an About Box. The goal is to press an About button on the wxPython File menu and generate an About Box.
This is my code so far:
import wx
import os
import pygame
pygame.init()
##SOUNDS##
goliathwav = pygame.mixer.Sound("goliath.wav")
channelopen = pygame.mixer.Sound("channelopen.wav")
##SOUNDS##
class windowClass(wx.Frame):
def __init__(self, *args, **kwargs):
super(windowClass,self).__init__(*args,**kwargs)
self.__basicGUI()
def __basicGUI(self):
panel = wx.Panel(self)
menuBar = wx.MenuBar()
fileButton = wx.Menu()
editButton = wx.Menu()
aboutBox = wx.MessageDialog(None, "Created by Kommander000(cyrex)")
answer=aboutBox.ShowModal()
aboutBox.Destroy()
aboutButton = wx.Menu()
exitItem = fileButton.Append(wx.ID_EXIT, 'Exit','status msg...')
aboutItem = aboutButton.Append(wx.ID_ABOUT, "About")
menuBar.Append(fileButton, 'File')
menuBar.Append(editButton, 'Edit')
menuBar.Append(aboutButton, 'About this program')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.__quit, exitItem)
self.Bind(wx.EVT_MENU, self.OnMenuHelpAbout, aboutBox)
self.__sound_dict = { "Goliath" : "goliath.wav",
"Goliath2" : "channelopen.wav"
}
self.__sound_list = sorted(self.__sound_dict.keys())
self.__list = wx.ListBox(panel,pos=(20,20), size=(250,150))
for i in self.__sound_list:
self.__list.Append(i)
self.__list.Bind(wx.EVT_LISTBOX,self.__on_click)
#wx.TextCtrl(panel,pos=(10,10), size=(250,150))
self.SetTitle("Soundboard")
self.Show(True)
def __on_click(self,event):
event.Skip()
name = self.__sound_list[self.__list.GetSelection()]
filename = self.__sound_dict[name]
if filename == "goliath.wav":
print "[ NOW PLAYING ] ... %s" % filename
pygame.mixer.Sound.play(goliathwav)
if filename == "channelopen.wav":
print "[ NOW PLAYING ] ... %s" % filename
pygame.mixer.Sound.play(channelopen)
def __quit(self, e):
self.Close()
def main():
app = wx.App()
windowClass(None, -1, style=wx.MAXIMIZE_BOX | wx.CAPTION | wx.CENTRE)
app.MainLoop()
main()
This is the error I am receiving:
self.Bind(wx.EVT_MENU, self.OnMenuHelpAbout, aboutBox)
AttributeError: 'windowClass' object has no attribute 'OnMenuHelpAbout'
Any suggestions?
Thanks as always,
kommander000
wx python has its own wx.AboutBox() used in conjunction with wx.AboutDialogInfo()
def About(self, event):
from platform import platform
myos = platform()
aboutInfo = wx.AboutDialogInfo()
aboutInfo.SetName("My Application ")
aboutInfo.SetVersion("1.0")
aboutInfo.SetDescription("My Super App," \
" That does amazing things\nRunning on: "+myos)
aboutInfo.SetCopyright("(C) Joe Bloggs-2016")
aboutInfo.SetLicense("https://www.gnu.org/licenses/gpl-2.0.html")
aboutInfo.AddDeveloper("Joe Bloggs")
aboutInfo.AddDocWriter("Joe Bloggs")
aboutInfo.SetWebSite('https://www.JoeBlogs.com')
wx.AboutBox(aboutInfo)
See:https://wxpython.org/docs/api/wx-module.html#AboutBox
you again :)
The aboutBox needs to be created in your missing callback OnMenuHelpAbout (that I refactored to make private as stated in my previous answer)
Also (not related) but I have changed the dictionary so it directly points to the sound object: no more if/elif, you can load 200 sounds the code will still be the same.
Fixed code:
import wx
import os
import pygame
pygame.init()
##SOUNDS##
##SOUNDS##
class windowClass(wx.Frame):
__goliathwav = pygame.mixer.Sound("goliath.wav")
__channelopen = pygame.mixer.Sound("channelopen.wav")
def __init__(self, *args, **kwargs):
super(windowClass,self).__init__(*args,**kwargs)
self.__basicGUI()
def __basicGUI(self):
panel = wx.Panel(self)
menuBar = wx.MenuBar()
fileButton = wx.Menu()
editButton = wx.Menu()
aboutButton = wx.Menu()
exitItem = fileButton.Append(wx.ID_EXIT, 'Exit','status msg...')
aboutItem = aboutButton.Append(wx.ID_ABOUT, "About")
menuBar.Append(fileButton, 'File')
menuBar.Append(editButton, 'Edit')
menuBar.Append(aboutButton, 'About this program')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.__quit, exitItem)
self.Bind(wx.EVT_MENU, self.__onmenuhelpabout, aboutItem)
self.__sound_dict = { "Goliath" : self.__goliathwav,
"Goliath2" : self.__channelopen
}
self.__sound_list = sorted(self.__sound_dict.keys())
self.__list = wx.ListBox(panel,pos=(20,20), size=(250,150))
for i in self.__sound_list:
self.__list.Append(i)
self.__list.Bind(wx.EVT_LISTBOX,self.__on_click)
#wx.TextCtrl(panel,pos=(10,10), size=(250,150))
self.SetTitle("Soundboard")
self.Show(True)
def __onmenuhelpabout(self,event):
event.Skip()
aboutBox = wx.MessageDialog(None, "Created by Kommander000(cyrex)")
answer=aboutBox.ShowModal()
aboutBox.Destroy()
def __on_click(self,event):
event.Skip()
name = self.__sound_list[self.__list.GetSelection()]
sound = self.__sound_dict[name]
print("[ NOW PLAYING ] ... %s" % name)
pygame.mixer.Sound.play(sound)
def __quit(self, e):
self.Close()
def main():
app = wx.App()
windowClass(None, -1, style=wx.MAXIMIZE_BOX | wx.CAPTION | wx.CENTRE)
app.MainLoop()
main()
def __quit(self, e):
self.Close()
def main():
app = wx.App()
windowClass(None, -1, style=wx.MAXIMIZE_BOX | wx.CAPTION | wx.CENTRE)
app.MainLoop()
main()

MplayerCtrl - MPlayer on Mac with WxPython

I am trying to get a simple media player working (scipt below) on Python 2.7.11, Wxpython, with MplayerCtrl on Mac 10.11.4, I just compiled the latest MPlayer 1.3.0, but when opening an mp4 video file I get the below error message, Mplayer opens independently (not in the player window) and also XQuartz opens for some reason, heres the error message:
Traceback (most recent call last):
File "/Users/me/Python/MediaPlayer/mediaplayer.py", line 124, in on_add_file
self.playbackSlider.SetRange(0, t_len)
File "/usr/local/lib/wxPython-3.0.2.0/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_controls.py", line 2866, in SetRange
return _controls_.Slider_SetRange(*args, **kwargs)
TypeError: in method 'Slider_SetRange', expected argument 3 of type 'int'
I have MPlayer 1.3.0 in the same directory as the .py script.
Here is the script I am using:
import os
import time
import wx
import MplayerCtrl as mpc
import wx.lib.buttons as buttons
dirName = os.path.dirname(os.path.abspath(__file__))
bitmapDir = os.path.join(dirName, 'bitmaps')
class Frame(wx.Frame):
#----------------------------------------------------------------------
def __init__(self, parent, id, title, mplayer):
wx.Frame.__init__(self, parent, id, title)
self.panel = wx.Panel(self)
sp = wx.StandardPaths.Get()
self.currentFolder = sp.GetDocumentsDir()
self.currentVolume = 50
self.create_menu()
# create sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
controlSizer = self.build_controls()
sliderSizer = wx.BoxSizer(wx.HORIZONTAL)
self.mplayer = mpc.MplayerCtrl(self.panel, -1, mplayer)
self.playbackSlider = wx.Slider(self.panel, size=wx.DefaultSize)
sliderSizer.Add(self.playbackSlider, 1, wx.ALL|wx.EXPAND, 5)
# create volume control
self.volumeCtrl = wx.Slider(self.panel)
self.volumeCtrl.SetRange(0, 100)
self.volumeCtrl.SetValue(self.currentVolume)
self.volumeCtrl.Bind(wx.EVT_SLIDER, self.on_set_volume)
controlSizer.Add(self.volumeCtrl, 0, wx.ALL, 5)
# create track counter
self.trackCounter = wx.StaticText(self.panel, label="00:00")
sliderSizer.Add(self.trackCounter, 0, wx.ALL|wx.CENTER, 5)
# set up playback timer
self.playbackTimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_update_playback)
mainSizer.Add(self.mplayer, 1, wx.ALL|wx.EXPAND, 5)
mainSizer.Add(sliderSizer, 0, wx.ALL|wx.EXPAND, 5)
mainSizer.Add(controlSizer, 0, wx.ALL|wx.CENTER, 5)
self.panel.SetSizer(mainSizer)
self.Bind(mpc.EVT_MEDIA_STARTED, self.on_media_started)
self.Bind(mpc.EVT_MEDIA_FINISHED, self.on_media_finished)
self.Bind(mpc.EVT_PROCESS_STARTED, self.on_process_started)
self.Bind(mpc.EVT_PROCESS_STOPPED, self.on_process_stopped)
self.Show()
self.panel.Layout()
#----------------------------------------------------------------------
def build_btn(self, btnDict, sizer):
""""""
bmp = btnDict['bitmap']
handler = btnDict['handler']
img = wx.Bitmap(os.path.join(bitmapDir, bmp))
btn = buttons.GenBitmapButton(self.panel, bitmap=img,
name=btnDict['name'])
btn.SetInitialSize()
btn.Bind(wx.EVT_BUTTON, handler)
sizer.Add(btn, 0, wx.LEFT, 3)
#----------------------------------------------------------------------
def build_controls(self):
"""
Builds the audio bar controls
"""
controlSizer = wx.BoxSizer(wx.HORIZONTAL)
btnData = [{'bitmap':'player_pause.png',
'handler':self.on_pause, 'name':'pause'},
{'bitmap':'player_stop.png',
'handler':self.on_stop, 'name':'stop'}]
for btn in btnData:
self.build_btn(btn, controlSizer)
return controlSizer
#----------------------------------------------------------------------
def create_menu(self):
"""
Creates a menu
"""
menubar = wx.MenuBar()
fileMenu = wx.Menu()
add_file_menu_item = fileMenu.Append(wx.NewId(), "&Add File", "Add Media File")
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.on_add_file, add_file_menu_item)
#----------------------------------------------------------------------
def on_add_file(self, event):
"""
Add a Movie and start playing it
"""
wildcard = "Media Files (*.*)|*.*"
dlg = wx.FileDialog(
self, message="Choose a file",
defaultDir=self.currentFolder,
defaultFile="",
wildcard=wildcard,
style=wx.OPEN | wx.CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
self.currentFolder = os.path.dirname(path[0])
trackPath = '"%s"' % path.replace("\\", "/")
self.mplayer.Loadfile(trackPath)
t_len = self.mplayer.GetTimeLength()
self.playbackSlider.SetRange(0, t_len)
self.playbackTimer.Start(100)
#----------------------------------------------------------------------
def on_media_started(self, event):
print 'Media started!'
#----------------------------------------------------------------------
def on_media_finished(self, event):
print 'Media finished!'
self.playbackTimer.Stop()
#----------------------------------------------------------------------
def on_pause(self, event):
""""""
if self.playbackTimer.IsRunning():
print "pausing..."
self.mplayer.Pause()
self.playbackTimer.Stop()
else:
print "unpausing..."
self.mplayer.Pause()
self.playbackTimer.Start()
#----------------------------------------------------------------------
def on_process_started(self, event):
print 'Process started!'
#----------------------------------------------------------------------
def on_process_stopped(self, event):
print 'Process stopped!'
#----------------------------------------------------------------------
def on_set_volume(self, event):
"""
Sets the volume of the music player
"""
self.currentVolume = self.volumeCtrl.GetValue()
self.mplayer.SetProperty("volume", self.currentVolume)
#----------------------------------------------------------------------
def on_stop(self, event):
""""""
print "stopping..."
self.mplayer.Stop()
self.playbackTimer.Stop()
#----------------------------------------------------------------------
def on_update_playback(self, event):
"""
Updates playback slider and track counter
"""
try:
offset = self.mplayer.GetTimePos()
except:
return
print offset
mod_off = str(offset)[-1]
if mod_off == '0':
print "mod_off"
offset = int(offset)
self.playbackSlider.SetValue(offset)
secsPlayed = time.strftime('%M:%S', time.gmtime(offset))
self.trackCounter.SetLabel(secsPlayed)
#----------------------------------------------------------------------
if __name__ == "__main__":
import os, sys
paths = [r'./mplayer',
r'./mplayer']
mplayerPath = None
for path in paths:
if os.path.exists(path):
mplayerPath = path
if not mplayerPath:
print "mplayer not found!"
sys.exit()
app = wx.App(redirect=False)
frame = Frame(None, -1, 'MediaPlayer Using MplayerCtrl', mplayerPath)
app.MainLoop()
I am keen on using MPlayer for its flexibility.

Simple wxPython post event from Process

I am trying to create a window that receives simple event notifications from a Process. Here is the code that I have so far:
import wx, wx.lib.newevent, time, sys
from multiprocessing import Process
size_width = 320
size_height = 240
background_color = (226,223,206)
SomeNewEvent, EVT_SOME_NEW_EVENT = wx.lib.newevent.NewEvent()
class StatusWindow(wx.Frame):
def __init__(self, parent):
super(StatusWindow, self).__init__(parent, title='Monitor', size=(size_width, size_height))
self.Bind(EVT_SOME_NEW_EVENT, self.updateStatus)
staticBox = wx.StaticBox(self, label='Monitor Status:', pos=(5, 105), size=(size_width - 28, size_height/3))
self.statusLabel = wx.StaticText(staticBox, label='None', pos=(10, 35), size=(size_width, 20), style=wx.ALIGN_LEFT)
self.count = 0
self.InitUI()
self.monitor = cMonitor()
self.monitor.start()
def InitUI(self):
panel = wx.Panel(self)
self.SetBackgroundColour(background_color)
self.Centre()
self.Show()
def updateStatus(self, evt):
self.statusLabel.SetLabel(evt.attr1)
class cMonitor(Process):
def __init__(self):
super(cMonitor, self).__init__()
def run(self):
time.sleep(2)
print 'This is an update'
#create the event
evt = SomeNewEvent(attr1="Some event has just occured")
#post the event
wx.PostEvent(EVT_SOME_NEW_EVENT, evt)
if __name__ == '__main__':
app = wx.App()
window = StatusWindow(None)
app.MainLoop()
The window gets created, but the Process does not appear to be either executing or sending the post event notification correctly. I should note that the print statement in the run method is not showing up either. What is causing the GUI to not be updated?? This was what I used as a reference:
http://wiki.wxpython.org/CustomEventClasses
First of all, your code throws an error:
Traceback (most recent call last):
File "C:\Python27\lib\multiprocessing\process.py", line 232, in _bootstrap
self.run()
File "C:\PyProgs\stackoverflow_answers\wx_answers\wx_events1.py", line 44, in run
wx.PostEvent(EVT_SOME_NEW_EVENT, evt)
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 8410, in PostEvent
return _core_.PostEvent(*args, **kwargs)
TypeError: in method 'PostEvent', expected argument 1 of type 'wxEvtHandler *'
According the docs, PostEvent(dest, event) send an event to a window or other wx.EvtHandler to be processed later, but in your code first parameter has type PyEventBinder. Your code would have to look something like this:
wx.PostEvent(self.wxWindow, evt)
where self.wxWindow - object of StatusWindow class. But there is another problem: you can not use wxPython objects as multiprocessor arguments(link).
One way to do what you want - using threading module instead multiprocessing:
import wx, wx.lib.newevent, time, sys
from threading import *
size_width = 320
size_height = 240
background_color = (226,223,206)
SomeNewEvent, EVT_SOME_NEW_EVENT = wx.lib.newevent.NewEvent()
class StatusWindow(wx.Frame):
def __init__(self, parent):
super(StatusWindow, self).__init__(parent, title='Monitor', size=(size_width, size_height))
self.Bind(EVT_SOME_NEW_EVENT, self.updateStatus)
staticBox = wx.StaticBox(self, label='Monitor Status:', pos=(5, 105), size=(size_width - 28, size_height/3))
self.statusLabel = wx.StaticText(staticBox, label='None', pos=(10, 35), size=(size_width, 20), style=wx.ALIGN_LEFT)
self.count = 0
self.InitUI()
# Set up event handler for any worker thread results
self.monitor = cMonitor(self)
self.monitor.start()
def InitUI(self):
panel = wx.Panel(self)
self.SetBackgroundColour(background_color)
self.Centre()
self.Show()
def updateStatus(self, evt):
self.statusLabel.SetLabel(evt.attr1)
class cMonitor(Thread):
def __init__(self, wxWindow):
super(cMonitor, self).__init__()
self.wxWindow = wxWindow
def run(self):
time.sleep(2)
print 'This is an update'
#create the event
evt = SomeNewEvent(attr1="Some event has just occured")
#post the event
wx.PostEvent(self.wxWindow, evt)
if __name__ == '__main__':
app = wx.App()
window = StatusWindow(None)
app.MainLoop()

Python thread runs even after closing WxPython application

the following code takes screenshots and logs keystrokes and mouse movements. It uses wxpython as GUI framework. I'm using python threads for screenshot and logging service. But whenever I close the GUI application. Threads are still running. How to stop those threads after closing the application?
import wx
import threading
import sys
import subprocess
import time
from pymouse import PyMouse
from pymouse import PyMouseEvent
from pykeyboard import PyKeyboard
from pykeyboard import PyKeyboardEvent
import pyscreenshot as ImageGrab
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import gtk.gdk
import urllib2, urllib
import Cookie
MouseMovesCount = 0
MouseClicksCount = 0
KeyStrokesCount = 0
class OD_App(wx.App):
def OnInit(self):
frame = OD_MainFrame ("Login", (0, 0), (350, 200))
self.SetTopWindow(frame)
loginPanel = OD_LoginPanel (frame)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
frame.Show()
return True
def OnCloseWindow (self, event):
self.Destroy()
class OD_MainFrame(wx.Frame):
def __init__(self, title, pos, size):
wx.Frame.__init__(self, None, -1, title, pos, size)
self.CreateStatusBar()
class OD_LoginPanel(wx.Panel):
def __init__(self, frame):
self.panel = wx.Panel(frame)
self.frame = frame
self.frame.SetStatusText("Authentication required!")
self.showLoginBox()
def showLoginBox (self):
# Create the sizer
sizer = wx.FlexGridSizer(rows=3, cols=2, hgap=5, vgap=15)
# Username
self.txt_Username = wx.TextCtrl(self.panel, 1, size=(150, -1))
lbl_Username = wx.StaticText(self.panel, -1, "Username:")
sizer.Add(lbl_Username,0, wx.LEFT|wx.TOP| wx.RIGHT, 50)
sizer.Add(self.txt_Username,0, wx.TOP| wx.RIGHT, 50)
# Password
self.txt_Password = wx.TextCtrl(self.panel, 1, size=(150, -1), style=wx.TE_PASSWORD)
lbl_Password = wx.StaticText(self.panel, -1, "Password:")
sizer.Add(lbl_Password,0, wx.LEFT|wx.RIGHT, 50)
sizer.Add(self.txt_Password,0, wx.RIGHT, 50)
# Submit button
btn_Process = wx.Button(self.panel, -1, "&Login")
self.panel.Bind(wx.EVT_BUTTON, self.OnSubmit, btn_Process)
sizer.Add(btn_Process,0, wx.LEFT, 50)
self.panel.SetSizer(sizer)
def OnSubmit(self, event):
username = self.txt_Username.GetValue()
password = self.txt_Password.GetValue()
mydata = [('username',username),('password',password)]
mydata = urllib.urlencode(mydata)
path = 'http://xyz/logincheck.php' #temporary db for testing
req = urllib2.Request(path, mydata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
page = urllib2.urlopen(req).read()
if page == "true":
self.frame.SetStatusText("Authentication Success!")
self.show_other(username)
else:
self.frame.SetStatusText("Authentication Failed!")
def OnCloseWindow (self, event):
self.Destroy()
def show_other(self,username):
self.frame.Destroy()
userpanel = OD_UserPanel()
return True
class OD_UserPanel(wx.App):
def OnInit(self):
userpanel = wx.Frame(None,-1)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
#user_greeting = 'Welcome ' + username + '!'
#username = wx.StaticText(userpanel, -1, user_greeting , style=wx.ALIGN_CENTRE)
userpanel.Show()
mouse_eventer = mouse_event()
mouse_eventer.start()
keyboard_eventer = key_event()
keyboard_eventer.start()
screenshot_eventer = screenshot_thread()
screenshot_eventer.start()
return True
def OnCloseWindow (self, event):
quit()
event.Skip()
raise SystemExit
class mouse_event(PyMouseEvent):
def move(self, x, y):
global MouseMovesCount
MouseMovesCount = MouseMovesCount + 1
print MouseMovesCount
def click(self, x, y, button, press):
global MouseClicksCount
if press:
MouseClicksCount = MouseClicksCount + 1
print MouseClicksCount
else:
MouseClicksCount = MouseClicksCount + 1
print MouseClicksCount
class key_event(PyKeyboardEvent):
global screenshot_eventer
def key_press(self, key):
global KeyStrokesCount
KeyStrokesCount = KeyStrokesCount + 1
print KeyStrokesCount
def key_release(self, key):
global KeyStrokesCount
KeyStrokesCount = KeyStrokesCount + 1
print KeyStrokesCount
class screenshot_thread(threading.Thread):
def __init__(self):
super(screenshot_thread, self).__init__()
self.state = True
# Attributes
def run(self):
self.take_shot()
def stop(self):
self.state = False
threading.Thread._Thread__stop()
def take_shot(self):
while self.state==True:
time.sleep(10)
subprocess.call(['scrot'])
if __name__ == '__main__':
app = OD_App()
app.MainLoop()
Don't call threading.Thread._Thread__stop! The leading underscore is a sign that this is internal api, it's not guaranteed to even exist (in fact, in python3 it's gone).
If you wish the thead to be destroyed automatically, set it to be daemonic:
def __init__(self):
super(screenshot_thread, self).__init__()
self.daemon = True
That will cause it to be automatically destroyed when the last non-daemonic thread has stopped. Or, in your case, just setting the state to False should make the thread exit after 10 seconds.
You defined a stop method in your screenshot_thread class but you does not use it. Calling it in the method OnCloseWindow should do the job.

wx Import Error

I am trying to run this code. Previously, it was giving No module wx as an error. Then I downloaded the wx module and now it is giving this error:
Traceback (most recent call last):
File "C:\Python24\player.py", line 2, in -toplevel-
import wx
File "C:\Python24\wx__init__.py", line 45, in -toplevel-
from wxPython import wx
File "C:\Python24\wxPython__init__.py", line 20, in -toplevel-
import wxc
ImportError: DLL load failed: The specified module could not be found.
Here is my code:
import os
import wx
import wx.media
import wx.lib.buttons as buttons
dirName = os.path.dirname(os.path.abspath(__file__))
bitmapDir = os.path.join(dirName, 'bitmaps')
########################################################################
class MediaPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
self.frame = parent
self.currentVolume = 50
self.createMenu()
self.layoutControls()
sp = wx.StandardPaths.Get()
self.currentFolder = sp.GetDocumentsDir()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.onTimer)
self.timer.Start(100)
#----------------------------------------------------------------------
def layoutControls(self):
"""
Create and layout the widgets
"""
try:
self.mediaPlayer = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)
except NotImplementedError:
self.Destroy()
raise
# create playback slider
self.playbackSlider = wx.Slider(self, size=wx.DefaultSize)
self.Bind(wx.EVT_SLIDER, self.onSeek, self.playbackSlider)
self.volumeCtrl = wx.Slider(self, style=wx.SL_VERTICAL|wx.SL_INVERSE)
self.volumeCtrl.SetRange(0, 100)
self.volumeCtrl.SetValue(self.currentVolume)
self.volumeCtrl.Bind(wx.EVT_SLIDER, self.onSetVolume)
# Create sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
audioSizer = self.buildAudioBar()
# layout widgets
mainSizer.Add(self.playbackSlider, 1, wx.ALL|wx.EXPAND, 5)
hSizer.Add(audioSizer, 0, wx.ALL|wx.CENTER, 5)
hSizer.Add(self.volumeCtrl, 0, wx.ALL, 5)
mainSizer.Add(hSizer)
self.SetSizer(mainSizer)
self.Layout()
#----------------------------------------------------------------------
def buildAudioBar(self):
"""
Builds the audio bar controls
"""
audioBarSizer = wx.BoxSizer(wx.HORIZONTAL)
self.buildBtn({'bitmap':'player_prev.png', 'handler':self.onPrev,
'name':'prev'},
audioBarSizer)
# create play/pause toggle button
img = wx.Bitmap(os.path.join(bitmapDir, "player_play.png"))
self.playPauseBtn = buttons.GenBitmapToggleButton(self, bitmap=img, name="play")
self.playPauseBtn.Enable(False)
img = wx.Bitmap(os.path.join(bitmapDir, "player_pause.png"))
self.playPauseBtn.SetBitmapSelected(img)
self.playPauseBtn.SetInitialSize()
self.playPauseBtn.Bind(wx.EVT_BUTTON, self.onPlay)
audioBarSizer.Add(self.playPauseBtn, 0, wx.LEFT, 3)
btnData = [{'bitmap':'player_stop.png',
'handler':self.onStop, 'name':'stop'},
{'bitmap':'player_next.png',
'handler':self.onNext, 'name':'next'}]
for btn in btnData:
self.buildBtn(btn, audioBarSizer)
return audioBarSizer
#----------------------------------------------------------------------
def buildBtn(self, btnDict, sizer):
""""""
bmp = btnDict['bitmap']
handler = btnDict['handler']
img = wx.Bitmap(os.path.join(bitmapDir, bmp))
btn = buttons.GenBitmapButton(self, bitmap=img, name=btnDict['name'])
btn.SetInitialSize()
btn.Bind(wx.EVT_BUTTON, handler)
sizer.Add(btn, 0, wx.LEFT, 3)
#----------------------------------------------------------------------
def createMenu(self):
"""
Creates a menu
"""
menubar = wx.MenuBar()
fileMenu = wx.Menu()
open_file_menu_item = fileMenu.Append(wx.NewId(), "&Open", "Open a File")
menubar.Append(fileMenu, '&File')
self.frame.SetMenuBar(menubar)
self.frame.Bind(wx.EVT_MENU, self.onBrowse, open_file_menu_item)
#----------------------------------------------------------------------
def loadMusic(self, musicFile):
"""
Load the music into the MediaCtrl or display an error dialog
if the user tries to load an unsupported file type
"""
if not self.mediaPlayer.Load(musicFile):
wx.MessageBox("Unable to load %s: Unsupported format?" % path,
"ERROR",
wx.ICON_ERROR | wx.OK)
else:
self.mediaPlayer.SetInitialSize()
self.GetSizer().Layout()
self.playbackSlider.SetRange(0, self.mediaPlayer.Length())
self.playPauseBtn.Enable(True)
#----------------------------------------------------------------------
def onBrowse(self, event):
"""
Opens file dialog to browse for music
"""
wildcard = "MP3 (*.mp3)|*.mp3|" \
"WAV (*.wav)|*.wav"
dlg = wx.FileDialog(
self, message="Choose a file",
defaultDir=self.currentFolder,
defaultFile="",
wildcard=wildcard,
style=wx.OPEN | wx.CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
self.currentFolder = os.path.dirname(path)
self.loadMusic(path)
dlg.Destroy()
#----------------------------------------------------------------------
def onNext(self, event):
"""
Not implemented!
"""
pass
#----------------------------------------------------------------------
def onPause(self):
"""
Pauses the music
"""
self.mediaPlayer.Pause()
#----------------------------------------------------------------------
def onPlay(self, event):
"""
Plays the music
"""
if not event.GetIsDown():
self.onPause()
return
if not self.mediaPlayer.Play():
wx.MessageBox("Unable to Play media : Unsupported format?",
"ERROR",
wx.ICON_ERROR | wx.OK)
else:
self.mediaPlayer.SetInitialSize()
self.GetSizer().Layout()
self.playbackSlider.SetRange(0, self.mediaPlayer.Length())
event.Skip()
#----------------------------------------------------------------------
def onPrev(self, event):
"""
Not implemented!
"""
pass
#----------------------------------------------------------------------
def onSeek(self, event):
"""
Seeks the media file according to the amount the slider has
been adjusted.
"""
offset = self.playbackSlider.GetValue()
self.mediaPlayer.Seek(offset)
#----------------------------------------------------------------------
def onSetVolume(self, event):
"""
Sets the volume of the music player
"""
self.currentVolume = self.volumeCtrl.GetValue()
print "setting volume to: %s" % int(self.currentVolume)
self.mediaPlayer.SetVolume(self.currentVolume)
#----------------------------------------------------------------------
def onStop(self, event):
"""
Stops the music and resets the play button
"""
self.mediaPlayer.Stop()
self.playPauseBtn.SetToggle(False)
#----------------------------------------------------------------------
def onTimer(self, event):
"""
Keeps the player slider updated
"""
offset = self.mediaPlayer.Tell()
self.playbackSlider.SetValue(offset)
########################################################################
class MediaFrame(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Python Music Player")
panel = MediaPanel(self)
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MediaFrame()
frame.Show()
app.MainLoop()
It may be that I didn't install wx correctly. I am using Python 2.4 and I was unable to find the wx module for Python 2.4. I downloaded wx for Python 2.5 and pasted the wx folder into the Python 2.4 directory. Can you please guide me how to add the wx module in Python 2.4?
Check here.
The 2.8.10 version still has binary installers for 2.4

Categories