python logo


Tag: wxpython

wxPython window

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 dialog

In this tutorial, you’ll learn how to display various types of dialogs using wxPython, a popular GUI toolkit for Python.
Dialogs play an essential role in user interaction, informing users or seeking confirmation on certain operations. With wxPython, showing these dialogs is straightforward.

Related Course: Creating GUI Applications with wxPython

Information Dialog in wxPython
An information dialog is a simple way to convey messages to users. Here’s how you can display one using wxPython:

1
2
3
4
import wx

app = wx.App()
wx.MessageBox('Pythonspot wxWidgets demo', 'Info', wx.OK | wx.ICON_INFORMATION)

The first parameter is the message text, the second specifies the dialog title, and the last one defines the dialog’s behavior and appearance.

wx dialog

Other wxPython Dialogs: Warning, Error, and Default
By tweaking the parameters, you can also create other dialog types. Let’s look at a few examples:

1
2
3
4
5
6
7
8
9
10
11
12
import wx

app = wx.App()

# Simple dialog
wx.MessageBox('A dialog', 'Title', wx.OK)

# Warning dialog
wx.MessageBox('Operation could not be completed', 'Warning', wx.OK | wx.ICON_WARNING)

# Error dialog
wx.MessageBox('Operation could not be completed', 'Error', wx.OK | wx.ICON_ERROR)

wxDialog

Creating a Question Dialog in wxPython
wxPython also allows for creating question dialogs, which can be used to seek yes/no responses from users:

1
2
3
4
5
6
7
8
9
10
11
import wx

app = wx.App()

dlg = wx.MessageDialog(None, "Do you want to update?",'Updater',wx.YES_NO | wx.ICON_QUESTION)
result = dlg.ShowModal()

if result == wx.ID_YES:
print("Yes pressed")
else:
print("No pressed")

wxDialog

Once again, remember that wxPython makes it convenient to create and manage GUI applications. The dialogs shown are just a small part of its capabilities.

Learn More: Creating GUI Applications with wxPython

Navigate the tutorial: [

wx python

While working with wxPython, it’s essential to grasp object orientation even if it hasn’t been the main focus of our series. This guide will help you understand how to design a tab interface with wxPython.

Recommended Course: Creating GUI Applications with wxPython

The Mainframe class constructs the frame, similar to previous examples. Other classes serve as the contents for the tabs. Within the main frame, we establish a panel and a notebook (which acts as the tab holder). Following that, we initiate tab objects:

tab1 = TabOne(nb)
tab2 = TabTwo(nb)
...

We then attach these tab objects to the tab holder:

nb.AddPage(tab1, "Tab 1")
nb.AddPage(tab2, "Tab 2")
...

Here’s the complete code for better clarity:

import wx

# Define the content of tabs as distinct classes:
class TabOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "Content of the first tab", (20,20))

class TabTwo(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "Content of the second tab", (20,20))

class TabThree(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "Content of the third tab", (20,20))

class TabFour(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "Content of the fourth tab", (20,20))

class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="wxPython tabbed interface example @pythonspot.com")

# Panel creation and tab holder setup:
p = wx.Panel(self)
nb = wx.Notebook(p)

# Initiation of the tab windows:
tab1 = TabOne(nb)
tab2 = TabTwo(nb)
tab3 = TabThree(nb)
tab4 = TabFour(nb)

# Assigning names to tabs and adding them:
nb.AddPage(tab1, "Tab 1")
nb.AddPage(tab2, "Tab 2")
nb.AddPage(tab3, "Tab 3")
nb.AddPage(tab4, "Tab 4")

# Organizing notebook layout using a sizer:
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)

if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()

Output Image:
wxTabs

Enrich your knowledge with: Creating GUI Applications with wxPython