Seite 1 von 1

WX Python: Menu-Items und Buttons über Loop generieren

Verfasst: Donnerstag 13. Dezember 2007, 13:43
von snakeseven
Hi,
da ich mich gerade in WX-Python einarbeite, teste ich ein paar Sachen aus, die ich auf jeden Fall brauchen werde. Und vieleicht kann ein anderer das ja auch gerade gebrauchen.

Gruß Seven

Checkbuttons per Loop generieren:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

'''
wx-python: 
creates any number checkboxes in a loop
'''

import wx
 
number_of_checkboxes = 5 

cboxlabels = ["%02d" % (i+1) for i in range (number_of_checkboxes)]

class MyFrame (wx.Frame):
    
    def __init__ (self, parent, id, title =''):
        wx.Frame.__init__ (self, parent, id, title, wx.DefaultPosition, wx.Size (number_of_checkboxes*50, number_of_checkboxes*35))
        panel = wx.Panel (self, -1)
        
        self.cboxlist = []
        
        off = 20
        for label in cboxlabels:
            self.cboxlist.append (wx.CheckBox (panel, -1, label, (10, 10 + off)))
            wx.EVT_CHECKBOX(self, self.cboxlist [cboxlabels.index (label)].GetId(), self.select)
            off += 20
            
        self.statusbar = self.CreateStatusBar (number_of_checkboxes)
        
 
    def select (self, event):
        for n in self.cboxlist: 
            state = str (n.GetValue())
            self.statusbar.SetStatusText (state, self.cboxlist.index(n))
            if state == 'True':
                self.SetTitle(cboxlabels[self.cboxlist.index(n)])
            
class MyApp(wx.App):
    
    def OnInit (self):
        frame = MyFrame (None, -1, '')
        frame.Show (True)
        frame.Center ()
        frame.cboxlist [0].SetValue (True)
        frame.select (True)
        return True
        
app = MyApp (0)
app.MainLoop ()
Radiobuttons per Loop generieren:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

'''
wx-python: 
creates any number of radiobuttons in a loop
'''

import wx
 
number_of_buttons = 5 

buttonlabels = ["%02d" % (i+1) for i in range (number_of_buttons)]

class MyFrame (wx.Frame):
    
    def __init__ (self, parent, id, title =''):
        wx.Frame.__init__ (self, parent, id, title, wx.DefaultPosition, wx.Size (number_of_buttons*50, number_of_buttons*35))
        panel = wx.Panel (self, -1)
        
        self.buttonlist = []
        
        off = 20
        for label in buttonlabels:
            self.buttonlist.append (wx.RadioButton (panel, -1, label, (10, 10 + off)))
            self.Bind (wx.EVT_RADIOBUTTON, self.select, id = self.buttonlist [buttonlabels.index (label)].GetId())
            off += 20
            
        self.statusbar = self.CreateStatusBar (number_of_buttons)
        
 
    def select (self, event):
        for n in self.buttonlist: 
            state = str (n.GetValue())
            self.statusbar.SetStatusText (state, self.buttonlist.index(n))
            
     
class MyApp(wx.App):
    
    def OnInit (self):
        frame = MyFrame (None, -1, '')
        frame.Show (True)
        frame.Center ()
        frame.buttonlist [1].SetValue (True)
        frame.select (True)
        return True
        
 
app = MyApp (0)
app.MainLoop ()
Menu-Items per Loop generieren:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

'''
wx-python: 
creates any number of (sub)menu items in a loop
'''

import wx

number_of_edititems = 5

edit_itemnames = ["%02d" % (i+1) for i in range (number_of_edititems)]
 
class MyMenu (wx.Frame):
    
    def __init__ (self, parent, id, title):
        wx.Frame.__init__ (self, parent, id, title, wx.DefaultPosition, wx.Size (200, 150))
 
        menubar = wx.MenuBar ()
        file = wx.Menu ()
        edit = wx.Menu ()
        file.Append (101, '&Open')
        file.Append (102, '&Save')
        file.AppendSeparator ()
        quit = wx.MenuItem (file, 111, '&Quit\tCtrl+Q')
        self.Bind (wx.EVT_MENU, self.endprogram, id=111)
        file.AppendItem (quit)
        
        self.submenu_edit = wx.Menu()
        
        itemID = 301
        for itemname in edit_itemnames:
            self.submenu_edit.Append (itemID, itemname, kind=wx.ITEM_CHECK)
            self.Bind (wx.EVT_MENU, self.show_selected_edititems, id=itemID)
            itemID += 1
            
        edit.AppendMenu (201, 'submenu_edit', self.submenu_edit)
 
        menubar.Append (file, '&File')
        menubar.Append (edit, '&Edit')
        self.SetMenuBar (menubar)
        
        self.statusbar = self.CreateStatusBar (number_of_edititems)
        
        
    def show_selected_edititems (self, event):
        for i in range(number_of_edititems):
            self.statusbar.SetStatusText (str (self.submenu_edit.IsChecked (i+301)), i)
        
    def endprogram(self, event):
        self.Close()
 
 
class MyApp (wx.App):
    
    def OnInit (self):
        frame = MyMenu (None, -1, '')
        frame.Show (True)
        return True
 
app = MyApp (0)
app.MainLoop ()