04 WxPython I
04 WxPython I
PyQt
Tinker
The wxPython GUI toolkit is a Python wrapper around a C++ library called
wxWidgets. The initial release of wxPython was in 1998, so wxPython has been
around quite a long time. wxPython’s primary difference from other toolkits, such
as PyQt or Tkinter, is that wxPython uses the actual widgets on the native platform
whenever possible. This makes wxPython applications look native to the operating
system that it is running on.
https://realpython.com/python-gui-w
ith-wxpython/
wxWidgets
wxWidgets is a C++ library that lets developers create applications for Windows,
macOS, Linux and other platforms with a single code base.
It has popular language bindings for Python, Ruby, Lua, Perl and several other
languages, and unlike other cross-platform toolkits, wxWidgets gives applications
a truly native look and feel because it uses the platform's native API rather than
emulating the GUI.
Installing wxPython
You can now use pip to install wxPython 4, which was not possible in the legacy
versions of wxPython. You can do the following to install it on your machine:
poetry add
wxPython@https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-22.04/
wxPython-4.2.1-cp310-cp310-linux_x86_64.whl
Definition of a GUI
As was mentioned in the introduction, a graphical user interface (GUI) is an
interface that is drawn on the screen for the user to interact with.
● Main window
● Menu
● Toolbar
● Buttons
● Text Entry
● Labels
Event Loops
A graphical user interface works by waiting for the user to do something. The
something is called an event. Events happen when the user types something while
your application is in focus or when the user uses their mouse to press a button or
other widget.
The wxPython framework has special thread-safe methods that you can use to
communicate back to your application to let it know that the thread is finished or to
give it an update.
Sample Skeleton
import wx
app=wx.App();
frame.Show()
app.MainLoop();
API
https://docs.wxpython.org
Add a Button
import wx
app=wx.App();
frame=wx.Frame(parent=None, title="Test", size=wx.Size(800, 600))
panel=wx.Panel(parent=frame)
button=wx.Button(parent=panel,
pos=wx.Point(200, 200),
label="Hello",
size=wx.Size(100, 50))
frame.Show()
app.MainLoop();
wx.Frame
A frame is a window whose size and position can (usually) be changed by the
user.
It usually has thick borders and a title bar, and can optionally contain a menu bar,
toolbar and status bar. A frame can contain any window that is not a frame or
dialog.
If a frame is to be used as an input form, the controls should not be created as its
children. Instead, a wx.Panel should be created as the sole child of the frame,
serving as the parent of the actual controls (the frame will size the panel so it
always fills its client area).
● parent (wx.Window) – The window parent. This may be, and often is, None. If it is not None,
the frame will be minimized when its parent is minimized and restored when it is restored
(although it will still be possible to minimize and restore just this frame itself).
● id (wx.WindowID) – The window identifier. It may take a value of -1 to indicate a default
value.
● title (string) – The caption to be displayed on the frame’s title bar.
● pos (wx.Point) – The window position. The value DefaultPosition indicates a default
position, chosen by either the windowing system or wxWidgets, depending on platform.
● size (wx.Size) – The window size. The value DefaultSize indicates a default size, chosen by
either the windowing system or wxWidgets, depending on platform.
● style (long) – The window style. See wx.Frame class description.
● name (string) – The name of the window. This parameter is used to associate a name with
the item, allowing the application user to set Motif resource values for individual windows.
Window Styles
● wx.DEFAULT_FRAME_STYLE: Defined as wx.MINIMIZE_BOX |
wx.MAXIMIZE_BOX | wx.RESIZE_BORDER | wx.SYSTEM_MENU |
wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN.
● wx.STAY_ON_TOP: Stay on top of all other windows, see also
wx.FRAME_FLOAT_ON_PARENT.
● wx.SYSTEM_MENU: Displays a system menu containing the list of various
windows commands in the window title bar. Unlike wx.MINIMIZE_BOX,
wx.MAXIMIZE_BOX and wx.CLOSE_BOX styles this style can be used
without wx.CAPTION, at least under Windows, and makes the system menu
available without showing it on screen in this case. However it is
recommended to only use it together with wx.CAPTION for consistent
behaviour under all platforms.
wxPanel
A panel is a window on which controls are placed.
It is usually placed within a frame. Its main feature over its parent class
wx.Window is code for handling child windows and TAB traversal, which is
implemented natively if possible (e.g. in wxGTK) or by wxWidgets itself otherwise.
● parent (wx.Window) – The parent window.
● id (wx.WindowID) – An identifier for the panel. ID_ANY is taken to mean a
default.
● pos (wx.Point) – The panel position. The value wx.DefaultPosition indicates a
default position, chosen by either the windowing system or wxWidgets,
depending on platform.
● size (wx.Size) – The panel size. The value wx.DefaultSize indicates a default
size, chosen by either the windowing system or wxWidgets, depending on
platform.
● style (long) – The window style. See wx.Panel.
● name (string) – Window name.
wx.Button
A button is a control that contains a text string, and is one of the most common
elements of a GUI.
button.Bind(wx.EVT_BUTTON, buttonClicked)
No print, Dialog
wx.Dialog
A dialog box is a window with a title bar and sometimes a system menu, which can
be moved around the screen.
It can contain controls and other windows and is often used to allow the user to
make some choice or to answer a question.
Dialogs usually contain either a single button allowing to close the dialog or two
buttons, one accepting the changes and the other one discarding them (such
button, if present, is automatically activated if the user presses the “Esc” key). By
default, buttons with the standard wx.ID_OK and wx.ID_CANCEL identifiers
behave as expected.
● parent (wx.Window) – Can be None, a frame or another dialog box.
● id (wx.WindowID) – An identifier for the dialog. A value of -1 is taken to mean a
default.
● title (string) – The title of the dialog.
● pos (wx.Point) – The dialog position. The value DefaultPosition indicates a default
position, chosen by either the windowing system or wxWidgets, depending on
platform.
● size (wx.Size) – The dialog size. The value DefaultSize indicates a default size,
chosen by either the windowing system or wxWidgets, depending on platform.
● style (long) – The window style.
● name (string) – Used to associate a name with the window, allowing the application
user to set Motif resource values for individual dialog boxes.
wx.StaticText
A static text control displays one or more lines of read-only text.
wx.StaticText supports the three classic text alignments, label ellipsization i.e.
replacing parts of the text with the ellipsis (“…”) if the label doesn’t fit into the
provided space and also formatting markup with wx.Control.SetLabelMarkup.
● parent (wx.Window) – Parent window. Should not be None.
● id (wx.WindowID) – Control identifier. A value of -1 denotes a default value.
● label (string) – Text label.
● pos (wx.Point) – Window position.
● size (wx.Size) – Window size.
● style (long) – Window style. See wx.StaticText.
● name (string) – Window name.
def buttonClicked(event):
panel1=wx.Panel(parent=dialog)
dialog.ShowModal();
Add TextCtrl
wx.TextCtrl
A text control allows text to be displayed and edited.
It may be single line or multi-line. Notice that a lot of methods of the text controls
are found in the base wx.TextEntry class which is a common base class for
wx.TextCtrl and other controls using a single line text entry field (e.g.
wx.ComboBox).
● parent (wx.Window) – Parent window. Should not be None.
● id (wx.WindowID) – Control identifier. A value of -1 denotes a default value.
● value (string) – Default text value.
● pos (wx.Point) – Text control position.
● size (wx.Size) – Text control size.
● style (long) – Window style. See wx.TextCtrl.
● validator (wx.Validator) – Window validator.
● name (string) – Window name.
text=wx.TextCtrl(parent=panel,
pos=wx.Point(100, 200),
size=wx.Size(90, 50))
def buttonClicked(event):
dialog=wx.Dialog(parent=frame, title="Hello", size=wx.Size(300, 300))
panel1=wx.Panel(parent=dialog)
label=wx.StaticText(parent=panel1, label="Hello "+text.Value)
dialog.ShowModal();
Use MessageDialog
wx.MessageDialog
This class represents a dialog that shows a single or multi-line message, with a
choice of wx.OK, Yes, No and Cancel buttons.
● parent (wx.Window) – Parent window.
● message (string) – Message to show in the dialog.
● caption (string) – The dialog title.
● style (long) – Combination of style flags described above.
● pos (wx.Point) – Dialog position (ignored under MSW).
def buttonClicked(event):
dialog=wx.MessageDialog(parent=frame,
style=wx.OK,
message=text.Value,
caption="Hello")
dialog.ShowModal();