python-snippet/gui/gui_example_wx.py

63 lines
2 KiB
Python
Raw Permalink Normal View History

"""From http://sebsauvage.net/python/gui/"""
import wx
class my_wx_app(wx.Frame):
"""This class holds the main window"""
def __init__(self, parent, id, title):
"""We use the parent constructor and keep track of our parent"""
wx.Frame.__init__(self, parent, id, title)
self.parent = parent
self.initialise()
def initialise(self):
"""To separate GUI from logic, we create widgets here"""
# Create the layout manager
sizer = wx.GridBagSizer()
# Create and keep a reference to a text entry
self.entry = wx.TextCtrl(self, -1, value=u"Entrer du texte ici.")
sizer.Add(self.entry, (0,0), (1,1), wx.EXPAND)
self.Bind(wx.EVT_TEXT_ENTER, self.OnPressEnter, self.entry)
# Create a button
button = wx.Button(self, -1, label="Cliques moi !")
sizer.Add(button, (0,1))
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, button)
# Create a label
self.label = wx.StaticText(self, -1, label=u"Salut !")
self.label.SetBackgroundColour(wx.BLUE)
self.label.SetForegroundColour(wx.WHITE)
sizer.Add(self.label, (1,0), (1,2), wx.EXPAND)
# Allow content to be resized with window
sizer.AddGrowableCol(0)
self.SetSizerAndFit(sizer)
# Enable resizing horizontally, not vertically
self.SetSizeHints(-1, self.GetSize().y, -1, self.GetSize().y)
# Default focus to the text entry
self.entry.SetFocus()
self.entry.SetSelection(-1, -1)
self.Show(True)
def OnButtonClick(self, event):
self.label.SetLabel(self.entry.GetValue() + " (bouton)")
self.entry.SetFocus()
self.entry.SetSelection(-1, -1)
def OnPressEnter(self, event):
self.label.SetLabel(self.entry.GetValue() + " (entrée)")
self.entry.SetFocus()
self.entry.SetSelection(-1, -1)
if __name__ == "__main__":
app = wx.App()
frame = my_wx_app(None, -1, "My wxWidget application")
app.MainLoop()