So what im trying to do is, if the user select his location then the listbox should be filled in with values. I always get NameError: global name 'listcomputers' is not defined but i dont understand why.
can anybody help me ?

import os
import wx

class MyForm(wx.Frame):
    def __init__(self):
        global location
        wx.Frame.__init__(self, None, wx.ID_ANY, "Remote Administrator v1.0", (500,500), (425,250))
        panel = wx.Panel(self, wx.ID_ANY)
        listcomputers_list = []
        listcomputers = wx.ListBox(panel, 1, (8,16), (120, 96), listcomputers_list, wx.LB_MULTIPLE)

        listcomputers.SetBackgroundColour(wx.Colour(255, 255, 255))
        listcomputers.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
        listcomputers.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        listcomputers.Bind(wx.EVT_LISTBOX, self.onListComputers)

        start = wx.Button(panel, -1, 'Start', (8,136), (75, 23))
        start.Bind(wx.EVT_BUTTON, self.onStart)

        timer = wx.Slider(panel, -1, 0, 0, 10, (136,48), (104, 45), wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_BOTTOM | wx.SL_RIGHT | wx.SL_LABELS)
        timer.Bind(wx.EVT_SLIDER, self.onAdjust)
        
        label = wx.StaticText(panel, -1, 'Timer:', (144,32), (100, 23))
        label.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
        label.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        
        RadioShutdown = wx.RadioButton(panel, -1, 'Shutdown', (136,88), (104, 24))
        RadioShutdown.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
        RadioShutdown.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        RadioShutdown.SetValue(0)
        RadioShutdown.Bind(wx.EVT_RADIOBUTTON, self.onRadio)

        RadioLogoff = wx.RadioButton(panel, -1, 'Logoff', (136,112), (104, 24))
        RadioLogoff.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
        RadioLogoff.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        RadioLogoff.SetValue(0)
        RadioLogoff.Bind(wx.EVT_RADIOBUTTON, self.onRadio)

        RadioReboot = wx.RadioButton(panel, -1, 'Reboot', (136,136), (104, 24))
        RadioReboot.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
        RadioReboot.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        RadioReboot.SetValue(0)
        RadioReboot.Bind(wx.EVT_RADIOBUTTON, self.onRadio)

        self.CreateStatusBar()
        
        loc_list = ['Atel','Sint-Andries','Permeke']
        loc = wx.ComboBox(panel, -1, 'Select Location', (269,21), (121, 21), loc_list)
        loc.SetBackgroundColour(wx.Colour(255, 255, 255))
        loc.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
        loc.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        loc.Bind(wx.EVT_COMBOBOX, self.onLocation)
        
        self.progressbar = wx.Gauge(panel, -1, 100, (264,128), (137, 17))
        self.progressbar.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        self.progressbar.SetValue(0)   

    def onLocation(self, event):
        get = event.GetEventObject()
        location = get.GetValue()
        self.Hide()
        self.Update()
        self.Refresh()
        self.Show()      
        self.SetStatusText(location)
        if location == "Atel":
            listcomputers.Append("PC1")
            listcomputers.Append("PC2")
            listcomputers.Append("PC3")
            listcomputers.Append("PC4")
            listcomputers.Append("PC5")
            listcomputers.Append("PC6")
            listcomputers.Append("PC7")
            listcomputers.Append("PC8")
            listcomputers.Append("PC9")
            listcomputers.Append("PC10")
            listcomputers.Append("PC11")
            listcomputers.Append("PC12")
        if location == "Sint-Andries":
            listcomputers.Append("PC1")
            listcomputers.Append("PC2")
            listcomputers.Append("PC3")
            listcomputers.Append("PC4")
            listcomputers.Append("PC5")
            listcomputers.Append("PC6")
            listcomputers.Append("PC7")
            listcomputers.Append("PC8")
            listcomputers.Append("PC8")
        if location == "Permeke":
            listcomputers.Append("PC1")
            listcomputers.Append("PC2")
            listcomputers.Append("PC3")
            listcomputers.Append("PC4")
            listcomputers.Append("PC5") 
      
    def onListComputers(self, event):
        lst = event.GetEventObject()
        global pclst
        PcName = lst.GetSelections()
        pclst = []
        for x in PcName:
            x = x + 1
            pclst.append(x)
        self.SetStatusText("Selected Computers: " + str(pclst))
    def onAdjust(self, event):
        adj = event.GetEventObject()
        global tmr
        tmr = adj.GetValue()
        self.SetStatusText("Timer set to: " + str(tmr))

    def onRadio(self, event):
        obj = event.GetEventObject()
        label = obj.GetLabel()
        self.SetStatusText("Selected " + label)
        global RadioResult
        if label == "Shutdown":
            RadioResult = "Shutdown"
        if label == "Logoff":
            RadioResult = "Logoff"
        if label == "Reboot":
            RadioResult = "Reboot"
        
        
    def onStart(self, event):
        obj = event.GetEventObject()
        label = obj.GetLabel()
        if label == "Start":
            self.SetStatusText("Trying to start with actions")
            try:
                RadioResult
                tmr
                pclst
                print RadioResult
                print tmr
                print pclst
            except NameError:
                self.SetStatusText("Failed starting actions")
                wx.MessageBox('Not all required fields are filled in!\nPlease correct this and try again.', 'Error')

                    

                
#---------------------------------------------------------------------------

#---------------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

Do global search and replace for listcomputers to self.listcomputers to save the value in the object.

You could use lot of refactoring, I would for example store the

wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif')

to one variabel to make it easier to change in future and to make code cleaner,

This block

if location == "Atel":
    listcomputers.Append("PC1")
    listcomputers.Append("PC2")
    listcomputers.Append("PC3")
    listcomputers.Append("PC4")
    listcomputers.Append("PC5")
    listcomputers.Append("PC6")
    listcomputers.Append("PC7")
    listcomputers.Append("PC8")
    listcomputers.Append("PC9")
    listcomputers.Append("PC10")
    listcomputers.Append("PC11")
    listcomputers.Append("PC12")
elif location == "Sint-Andries": ## change to elif not if

Can be produced simpler with:

dictcomputers={ "Atel": [('PC%i' % n) for n in range(1,1+12)],
                "Sint-Andries": [('PC%i' % n) for n in range(1,1+8)],
                "Permeke": [('PC%i' % n) for n in range(1,1+5)] }
>> dictcomputers
{'Atel': ['PC1', 'PC2', 'PC3', 'PC4', 'PC5', 'PC6', 'PC7', 'PC8', 'PC9', 'PC10', 'PC11', 'PC12'], 'Sint-Andries': ['PC1', 'PC2', 'PC3', 'PC4', 'PC5', 'PC6', 'PC7', 'PC8'], 'Permeke': ['PC1', 'PC2', 'PC3', 'PC4', 'PC5']}

Then you used dictcomputers[location] as list in wx.ListBox self.listcomputers

You should define this in init or globally and replace loc_list with dictcomputers.keys().
Why line 6, I do not see you changing location anywhere in init?

Edited 6 Years Ago by pyTony: n/a

when i change the listcomputers into self.listcomputers i get this :

AttributeError: 'MyForm' object has no attribute 'listcomputers'

im new to python, i just dont understand that error, i mean i have the listcomputers just a few lines above that error ?
Also thanks for the dictionary sample, i needed that, but in another way i think. The computer names need to be changed, i want to display PC1 in the listbox, but then i want PC1 give me the computer's network name as result.
My guess was using either a dictionary or 2 lists but i think the dictionary will be easyer

It worked for me to get code running. I worked it out little as I have not still learned wx properly. Figure out the dictionary yourself mapping to names yourself, you need also consider location in addition to the list value.

import os
import wx

class MyForm(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Remote Administrator v1.0", (500,500), (425,280))

        self.font = wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif')
        self.dictcomputers={ u"Atel": [(u'PC%i' % n) for n in range(1,1+12)],
                u"Sint-Andries": [(u'PC%i' % n) for n in range(1,1+8)],
                u"Permee": [(u'PC%i' % n) for n in range(1,1+5)] }

        self.panel = wx.Panel(self, wx.ID_ANY)
        self.listcomputers = wx.ListBox(self.panel, 1, (8,16), (120, 150), [], wx.LB_MULTIPLE)

        self.listcomputers.SetBackgroundColour(wx.Colour(255, 255, 255))
        self.listcomputers.SetFont(self.font)
        self.listcomputers.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        self.listcomputers.Bind(wx.EVT_LISTBOX, self.onlistcomputers)

        start = wx.Button(self.panel, -1, 'Start', (8,176), (75, 23))
        start.Bind(wx.EVT_BUTTON, self.onStart)

        timer = wx.Slider(self.panel, -1, 0, 0, 10, (136,48), (104, 45), wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_BOTTOM | wx.SL_RIGHT | wx.SL_LABELS)
        timer.Bind(wx.EVT_SLIDER, self.onAdjust)
        
        label = wx.StaticText(self.panel, -1, 'Timer:', (144,32), (100, 23))
        label.SetFont(self.font)
        label.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        
        RadioShutdown = wx.RadioButton(self.panel, -1, 'Shutdown', (136,88), (104, 24))
        RadioShutdown.SetFont(self.font)
        RadioShutdown.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        RadioShutdown.SetValue(0)
        RadioShutdown.Bind(wx.EVT_RADIOBUTTON, self.onRadio)

        RadioLogoff = wx.RadioButton(self.panel, -1, 'Logoff', (136,112), (104, 24))
        RadioLogoff.SetFont(self.font)
        RadioLogoff.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        RadioLogoff.SetValue(0)
        RadioLogoff.Bind(wx.EVT_RADIOBUTTON, self.onRadio)

        RadioReboot = wx.RadioButton(self.panel, -1, 'Reboot', (136,136), (104, 24))
        RadioReboot.SetFont(self.font)
        RadioReboot.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        RadioReboot.SetValue(0)
        RadioReboot.Bind(wx.EVT_RADIOBUTTON, self.onRadio)

        self.CreateStatusBar()
        
        loc = wx.ComboBox(self.panel, -1, 'Select Location', (269,21), (121, 21), self.dictcomputers.keys())
        loc.SetBackgroundColour(wx.Colour(255, 255, 255))
        loc.SetFont(self.font)
        loc.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        loc.Bind(wx.EVT_COMBOBOX, self.onLocation)
        
        self.progressbar = wx.Gauge(self.panel, -1, 100, (264,128), (137, 17))
        self.progressbar.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
        self.progressbar.SetValue(0)   

    def onLocation(self, event):
        #get = event.GetEventObject() ## get is badly chosen name!
        location = event.GetEventObject().GetValue()
        self.Hide()
        self.Update()
        self.Refresh()
        self.Show()      
        self.SetStatusText(location)
        self.listcomputers.Set(self.dictcomputers[location])
      
    def onlistcomputers(self, event):
        lst = event.GetEventObject()
        PcName = lst.GetSelections()
        self.pclst = []
        for x in PcName:
            x = x + 1
            self.pclst.append(x)
        self.SetStatusText("Selected Computers: " + str(self.pclst))
        
    def onAdjust(self, event):
        adj = event.GetEventObject()
        self.tmr = adj.GetValue()
        self.SetStatusText("Timer set to: " + str(self.tmr))

    def onRadio(self, event):
        obj = event.GetEventObject()
        label = obj.GetLabel()
        self.SetStatusText("Selected " + label)
        if label == "Shutdown":
            self.RadioResult = "Shutdown"
        if label == "Logoff":
            self.RadioResult = "Logoff"
        if label == "Reboot":
            self.RadioResult = "Reboot"
               
    def onStart(self, event):
        obj = event.GetEventObject()
        label = obj.GetLabel()
        if label == "Start":
            try:
                print self.RadioResult
                print self.tmr
                if not self.pclst[0]:
                    raise IndexError
            except AttributeError:
                self.SetStatusText("Failed starting actions")
                wx.MessageBox('Not all required fields are filled in!\nPlease correct this and try again.', 'Error')
            except IndexError:
                self.SetStatusText("Failed starting actions")
                wx.MessageBox('No PC selected!\nPlease correct this and try again.', 'Error')
            else:
                self.SetStatusText("Trying to start with actions")


if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

awesome this works fine for me ill sort out the dictionary thanks alot !

This question has already been answered. Start a new discussion instead.