eEcho blog

is een halte van de gedachte

Toolbars wxpython

Menu’s is een groep van alle commando’s die we kunnen gebruiken in een toepassing. Werkbalken zorgen voor een snelle toegang tot de meest gebruikte commando’s.

simpletoolbar.jpg

#!/usr/bin/python

# simpletoolbar.py

import wx

class SimpleToolbar(wx.Frame):
    def __init__(self, parent, id, title):
       wx.Frame.__init__(self, parent, id, title, size=(300, 200))

       toolbar = self.CreateToolBar()
       toolbar.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap('../icons/exit.png'))
toolbar.Realize()

       self.Bind(wx.EVT_TOOL, self.OnExit, id=wx.ID_EXIT)

       self.Centre()
       self.Show(True)

    def OnExit(self, event):
       self.Close()

app = wx.App()
SimpleToolbar(None, -1, 'simple toolbar')
app.MainLoop()

#!/usr/bin/python

# toolbars.py

import wx

class Toolbars(wx.Frame):
    def __init__(self, parent, id, title):
       wx.Frame.__init__(self, parent, id, title, size=(300, 200))

       vbox = wx.BoxSizer(wx.VERTICAL)

       toolbar1 = wx.ToolBar(self, -1)
       toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/new.png'))
       toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/open.png'))
       toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/save.png'))
       toolbar1.Realize()

       toolbar2 = wx.ToolBar(self, -1)
       toolbar2.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap('../icons/exit.png'))
       toolbar2.Realize()

       vbox.Add(toolbar1, 0, wx.EXPAND)
       vbox.Add(toolbar2, 0, wx.EXPAND)

       self.Bind(wx.EVT_TOOL, self.OnExit, id=wx.ID_EXIT)

       self.SetSizer(vbox)
       self.Centre()
       self.Show(True)

    def OnExit(self, event):
       self.Close()

app = wx.App()
Toolbars(None, -1, 'toolbars')
app.MainLoop()

Soms moeten we een verticale werkbalk hebben. Verticale werkbalken worden vaak gebruikt in grafische toepassingen zoals Inkscape of Xara Xtreme.

verticaltoolbar.jpg

#!/usr/bin/python

# verticaltoolbar.py

import wx

class VerticalToolbar(wx.Frame):
    def __init__(self, parent, id, title):
       wx.Frame.__init__(self, parent, id, title, size=(240, 300))

       toolbar = self.CreateToolBar(wx.TB_VERTICAL)
       toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/select.gif'))
       toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/freehand.gif'))
       toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/shapeed.gif'))
       toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/pen.gif'))
       toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/rectangle.gif'))
       toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/ellipse.gif'))
       toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/qs.gif'))
       toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/text.gif'))

       toolbar.Realize()

       self.Centre()
       self.Show(True)

    def OnExit(self, event):
       self.Close()

app = wx.App()
VerticalToolbar(None, -1, 'vertical toolbar')
app.MainLoop()

In het volgende voorbeeld, zullen we laten zien, hoe we het kunnen in-en uitschakelen werkbalk knoppen ook een separator.

#!/usr/bin/python

# enabledisable.py

import wx

class EnableDisable(wx.Frame):
    def __init__(self, parent, id, title):
       wx.Frame.__init__(self, parent, id, title, size=(250, 150))

       self.count = 5

       self.toolbar = self.CreateToolBar()
       self.toolbar.AddLabelTool(wx.ID_UNDO, '', wx.Bitmap('../icons/undo.png'))
       self.toolbar.AddLabelTool(wx.ID_REDO, '', wx.Bitmap('../icons/redo.png'))
       self.toolbar.EnableTool(wx.ID_REDO, False)
       self.toolbar.AddSeparator()
       self.toolbar.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap('../icons/exit.png'))
       self.toolbar.Realize()

       self.Bind(wx.EVT_TOOL, self.OnExit, id=wx.ID_EXIT)
       self.Bind(wx.EVT_TOOL, self.OnUndo, id=wx.ID_UNDO)
       self.Bind(wx.EVT_TOOL, self.OnRedo, id=wx.ID_REDO)

       self.Centre()
       self.Show(True)

    def OnUndo(self, event):
       if self.count > 1 and self.count <= 5:
            self.count = self.count - 1

       if self.count == 1:
            self.toolbar.EnableTool(wx.ID_UNDO, False)

       if self.count == 4:
            self.toolbar.EnableTool(wx.ID_REDO, True)

    def OnRedo(self, event):
       if self.count < 5 and self.count >= 1:
            self.count = self.count + 1

       if self.count == 5:
            self.toolbar.EnableTool(wx.ID_REDO, False)

       if self.count == 2:
            self.toolbar.EnableTool(wx.ID_UNDO, True)

    def OnExit(self, event):
       self.Close()

app = wx.App()
EnableDisable(None, -1, 'enable disable')
app.MainLoop()

Comments are closed.

Home | info@eecho.info | Voorwaarden | Blog