python logo


Tag: wxwidgets

wxpython button

To create a button simply call wx.Button(). When creating a button with wx.Button() it is important to parse the panel as first argument. We attach it to a panel because attaching to the frame would make it full screen.

A panel gives you to option to position widgets anywhere in the window. The parameter (10,10) is the position on the panel. The id argument is necessary, but it is equal to -1 (wx.ID_ANY == -1). The 3rd parameter is the text on the button.

Related course:
Creating GUI Applications with wxPython

You can use the code below to create a button in wxPython:

#!/usr/bin/python

import wx

def onButton(event):
print "Button pressed."

app = wx.App()
frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0,0,200,50)

panel = wx.Panel(frame, wx.ID_ANY)
button = wx.Button(panel, wx.ID_ANY, 'Test', (10, 10))
button.Bind(wx.EVT_BUTTON, onButton)

frame.Show()
frame.Centre()
app.MainLoop()

The function onButton() is called if the button is pressed. We bind (connect) it with button.Bind(wx.EVT_BUTTON, onButton).

Output:

wx button Buton created with wxPython

Image on button
wxPython supports having images on buttons. Only a minor change is needed to display an image on a button. While the function is called wx.BitmapButton, it supports other image formats.

bmp = wx.Bitmap("call-start.png", wx.BITMAP_TYPE_ANY)
button = wx.BitmapButton(panel, id=wx.ID_ANY, bitmap=bmp,
size=(bmp.GetWidth()+10, bmp.GetHeight()+10))

The first line loads the image, the second line creates the button.

Full code:

#!/usr/bin/python

import wx

def onButton(event):
print "Button pressed."

app = wx.App()
frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0,0,200,70)
panel = wx.Panel(frame, wx.ID_ANY)

bmp = wx.Bitmap("call-start.png", wx.BITMAP_TYPE_ANY)
button = wx.BitmapButton(panel, id=wx.ID_ANY, bitmap=bmp,
size=(bmp.GetWidth()+10, bmp.GetHeight()+10))

button.Bind(wx.EVT_BUTTON, onButton)
button.SetPosition((10,10))

frame.Show()
frame.Centre()
app.MainLoop()

Output:

wxButton wxButton

Related course:
Creating GUI Applications with wxPython

 

wxPython window

wxPython is a GUI toolkit for the Python programming language. wxPython can be used to create graphical user interfaces (GUI).

Applications made with wxPython have a native appearance on all platforms. The application will appear as native application unlike QT or Tk which have a custom QT or Tk look. It runs on all major desktop platforms.

Currently supported operating systems are Microsoft Windows (32-bit), most Unix or unix-like systems, and Macintosh OS X.

Related course: Creating GUI Applications with wxPython

The wxPython module is based on the C++ GUI library wxWidgets.

wxPython window


To open a window with wxPython, run the code below:

#!/usr/bin/python

import wx
app = wx.App()
frame = wx.Frame(None, -1, 'win.py')
frame.Show()
app.MainLoop()

The line wx.App() creates an application object. Each wx program needs to have one .App() object.

The method wx.Frame() returns a new window which can contain widgets.

app.Mainloop() puts the application in the main loop and listens for events.

Window size and position
You can set the position and size with the SetDimensions() function:

#!/usr/bin/python

import wx

app = wx.App()
frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0,0,640,480)
frame.Show()
app.MainLoop()

The parameters of the function are: x (left), y (top), width and height. The function not only sets the screen resolution but also the position on the screen.

Center a window
To put the window in the center of the screen call:

frame.Centre()

Related course: Creating GUI Applications with wxPython
 

python dialog box input

Input dialogs let your user give you feedback or input. They appear in desktop applications once in a while.

wxPython supports input dialogs, they are included with the framework.
A typical wxPython dialog may look like:

wx input input dialog made with wxPython

Related course: Creating GUI Applications with wxPython

wxPython input dialog


The example code below creates an input dialog with wxPython:

#!/usr/bin/python

import wx

def onButton(event):
print "Button pressed."

app = wx.App()

frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0,0,200,50)

# Create text input
dlg = wx.TextEntryDialog(frame, 'Enter some text','Text Entry')
dlg.SetValue("Default")
if dlg.ShowModal() == wx.ID_OK:
print('You entered: %s\n' % dlg.GetValue())
dlg.Destroy()

A wxPython textbox can be added to a window using the function:


wx.TextEntryDialog(frame, 'Enter some text','Text Entry')

Where the first argument is the frame, the second argument is the label and the last argument is the window title.

The function below displays the dialog and waits for a user to press one of the buttons:


dlg.ShowModal()

You can get the button pressed by picking one of these:


wx.OK
wx.CANCEL

(the result is either one of these)

After input is been given, you can get the input text using dlg.GetValue() function.

Related course: Creating GUI Applications with wxPython