[wxPython] Recalling widget inside a function

Post here if you need help with creating a Graphical User Interface in Python.

[wxPython] Recalling widget inside a function

Postby Mograine » Thu Dec 12, 2013 3:52 am

This code:

Code: Select all
#-------------------------------------------------------------------------------
# Name:        modulo1
# Purpose:
#
# Author:      Marco
#
# Created:     11/12/2013
# Copyright:   (c) Marco 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------

import wx

class main(wx.Frame):
     def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, pos=(150, 150),
        size=(500, 400))
        #Panel
        panel = wx.Panel(self, 2, (-1, -1))
        #ListCtrl
        listctrl = wx.ListCtrl(panel, 3, (-1, 25), (-1, -1),
                                        style=wx.LC_REPORT|
                                        wx.LC_HRULES|
                                        wx.LC_VRULES)
        #NumericUpDown
        numericupdown = wx.SpinCtrl(panel, 5, "", (30, 200), (50, -1))
        numericupdown.SetRange(1,100)
        numericupdown.SetValue(5)
        #Button
        btn1 = wx.Button(panel, 5, "Create Columns", (30, 225), (-1, -1))
        self.Bind(wx.EVT_BUTTON, self.Onbtn1, btn1)

     def Onbtn1(self, evt):
        """Event handler for the button click."""
        i = 0
        g = numericupdown.GetValue()
        while i<g:
            label = 'Fascia' + str(i + 1)
            listctrl.InsertColumn(i, label)
            i = i + 1

# Run the program

class MyApp(wx.App):
    def OnInit(self):
        frame = main(None, "Simple wxPython App")
        self.SetTopWindow(frame)

        #print "Print statements go to this stdout window by default."

        frame.Show(True)
        return True

app = MyApp(redirect=True)
app.MainLoop()


Returns me this error when I push the button:

Traceback (most recent call last):
File "C:\Users\Marco\Documents\Python\4.py", line 38, in Onbtn1
g = numericupdown.GetValue()
NameError: global name 'numericupdown' is not defined


I suppose I should call the widget in a different way: i try with self.numericupdown but dont't work
User avatar
Mograine
 
Posts: 5
Joined: Tue Dec 10, 2013 2:47 pm

Re: [wxPython] Recalling widget inside a function

Postby Yoriz » Thu Dec 12, 2013 6:50 am

You need to use
Code: Select all
self.numericupdown

in both the __init__ and the Onbtn1 methods.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 841
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to GUI

Who is online

Users browsing this forum: No registered users and 3 guests