Open In App
Related Articles

Button in wxPython – Python

Improve Article
Improve
Save Article
Save
Like Article
Like

In this article we are going to learn how can we add buttons to a frame in wxPython. This can be done by  using the Button() constructor of wx.Button class.

Following styles are supported in this class:

  • wx.BU_LEFT: Left-justifies the label. Windows and GTK+ only.
  • wx.BU_TOP: Aligns the label to the top of the button. Windows and GTK+ only.
  • wx.BU_RIGHT: Right-justifies the bitmap label. Windows and GTK+ only.
  • wx.BU_BOTTOM: Aligns the label to the bottom of the button. Windows and GTK+ only.
  • wx.BU_EXACTFIT: By default, all buttons are made of at least the standard button size, even if their contents is small enough to fit into a smaller size. This is done for consistency as most platforms use buttons of the same size in the native dialogs, but can be overridden by specifying this flag. If it is given, the button will be made just big enough for its contents. Notice that under MSW the button will still have at least the standard height, even with this style, if it has a non-empty label.
  • wx.BU_NOTEXT: Disables the display of the text label in the button even if it has one or its id is one of the standard stock ids with an associated label: without using this style a button which is only supposed to show a bitmap but uses a standard id would display a label too.
  • wx.BORDER_NONE: Creates a button without border. This is currently implemented in MSW, GTK2 and OSX/Cocoa.

Syntax :

wx.StaticText(self, parent, id=ID_ANY, label=””, 
              pos=DefaultPosition, size=DefaultSize, 
                 style=0, validator= DefaultValidator, 
                            name=StaticTextNameStr)

Parameters :

Parameter Input Type Description
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.Window Window size.
style long Window style.
validator wx.Validator Window validator.
name string Window name.

Example #1: 

Python3




# import wxPython
def onButton(event):
    print( "Button pressed.")
 
app = wx.App()
frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(200, 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()
app.MainLoop()


Output :

 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 24 Feb, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials