Skip to content

WxPython

A blending of the wxWidgets C++ class library with the Python programming language.

How to install

## Ubuntu 18.04:
sudo apt-get install make gcc
sudo apt-get install libgtk2.0-dev libgtk-3-dev
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install libjpeg-dev libtiff-dev
sudo apt-get install libsdl1.2-dev
sudo apt-get install libnotify-dev freeglut3 freeglut3-dev libsm-dev
sudo apt-get install libwebkitgtk-dev libwebkitgtk-3.0-dev

wxPython Phoenix

(Summer 2016) The wxPython Phoenix train is back on the tracks and moving forward at full steam ahead!

wxPython's Project Phoenix. A new implementation of wxPython, better, stronger, faster than he was before.

Simple example

import wx

app = wx.App()

frame = wx.Frame(None, -1, 'simple.py')
frame.Show()

app.MainLoop()

OepnCV and wxPython example

import wx
import cv, cv2

class ShowCapture(wx.Panel):
    def __init__(self, parent, capture, fps=15):
        wx.Panel.__init__(self, parent)

        self.capture = capture
        ret, frame = self.capture.read()

        height, width = frame.shape[:2]
        parent.SetSize((width, height))
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        self.bmp = wx.BitmapFromBuffer(width, height, frame)

        self.timer = wx.Timer(self)
        self.timer.Start(1000./fps)

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_TIMER, self.NextFrame)


    def OnPaint(self, evt):
        dc = wx.BufferedPaintDC(self)
        dc.DrawBitmap(self.bmp, 0, 0)

    def NextFrame(self, event):
        ret, frame = self.capture.read()
        if ret:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            self.bmp.CopyFromBuffer(frame)
            self.Refresh()


capture = cv2.VideoCapture(0)
capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, 320)
capture.set(cv.CV_CAP_PROP_FRAME_HEIGHT, 240)

app = wx.App()
frame = wx.Frame(None)
cap = ShowCapture(frame, capture)
frame.Show()
app.MainLoop()

Directory Open Dialog

dialog = wx.DirDialog(self, "Choose input directory", "", wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
if dialog.ShowModal() != wx.ID_OK:
    pass
current_directory = dialog.GetPath()

Open file dialog

The typical usage for the open file dialog is:

def OnOpen(self, event):

    if self.contentNotSaved:
        if wx.MessageBox("Current content has not been saved! Proceed?", "Please confirm",
                         wx.ICON_QUESTION | wx.YES_NO, self) == wx.NO:
            return

    # otherwise ask the user what new file to open
    with wx.FileDialog(self, "Open XYZ file", wildcard="XYZ files (*.xyz)|*.xyz",
                       style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

        if fileDialog.ShowModal() == wx.ID_CANCEL:
            return     # the user changed their mind

        # Proceed loading the file chosen by the user
        pathname = fileDialog.GetPath()
        try:
            with open(pathname, 'r') as file:
                self.doLoadDataOrWhatever(file)
        except IOError:
            wx.LogError("Cannot open file '%s'." % newfile)

Save file dialog

The typical usage for the save file dialog is instead somewhat simpler:

def OnSaveAs(self, event):

    with wx.FileDialog(self, "Save XYZ file", wildcard="XYZ files (*.xyz)|*.xyz",
                       style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:

        if fileDialog.ShowModal() == wx.ID_CANCEL:
            return     # the user changed their mind

        # save the current contents in the file
        pathname = fileDialog.GetPath()
        try:
            with open(pathname, 'w') as file:
                self.doSaveData(file)
        except IOError:
            wx.LogError("Cannot save current data in file '%s'." % pathname)

Custom event

The previous version of this page has been subsumed by built-in wxPython functionality. As specified in the wxPython demo...

import  wx
import  wx.lib.newevent

SomeNewEvent, EVT_SOME_NEW_EVENT = wx.lib.newevent.NewEvent()
SomeNewCommandEvent, EVT_SOME_NEW_COMMAND_EVENT = wx.lib.newevent.NewCommandEvent()

You can bind the events normally via either binding syntax.

self.Bind(EVT_SOME_NEW_EVENT, self.handler)
EVT_SOME_NEW_EVENT(self, self.handler)

You can also attach arbitrary data to the event during its creation, then post it to whatever window you choose.

    #create the event
    evt = SomeNewEvent(attr1="hello", attr2=654)
    #post the event
    wx.PostEvent(target, evt)

When handling events with such arbitrary data, you can fetch the data via attributes, named the same as the names passed in during the event instance creation. That is, given the two keyword arguments passed to SomeNewEvent above...

    def handler(self, evt):
        # given the above constructed event, the following is true
        #evt.attr1 == "hello"
        #evt.attr2 == 654

Message Box

Box:

wx.MessageBox('Download completed', 'Info', wx.OK | wx.ICON_INFORMATION)

Dialog:

dial = wx.MessageDialog(None, 'Unallowed operation', 'Exclamation', wx.OK | wx.ICON_EXCLAMATION)
dial.ShowModal()

matplotlib with wxPython example

Matplotlib#matplotlib with wxPython example 항목 참조.

Troubleshooting

Just not working !!!

아무런 이유없이 작동하지 않을 경우 아래와 같은 조건을 확인해 보자.

  • matplotlib과 함께 사용할 경우 (라이브러리 충돌이 발생될 수 있다) 관련 import를 모두 제거해야 한다.

Local Download

Phoenix master 8304ec1 (2017-01-24)
Phoenix-master-8304ec1.zip

See also

Favorite site

Tutorials

Guide

References


  1. Zetcode.com-170124.zip 

  2. WxPython_Phoenix_Snapshot_Builds.txt.zip