I am writing a small application using Tkinter and Pmw. Using the Pmw NoteBook class have an interface to allow the user to create new tabs for input.
Each of these tabs has exactly the same content (radio buttons and entry fields), so I want the content to be written in just one function. But this does not work and I do not understand why not. I get a NameError in the Traceback that does not make sense to me, as “page” has been defined for the whole class, and I can use it in other function definitions.
<type 'exceptions.NameError'>: global name 'page' is not defined
The simplest working version of what I am trying to do is (based on the NoteBook_2 example shipped with Pmw):
import Tkinter
import Pmw
class Demo:
def __init__(self, parent):
self.pageCounter = 0
self.mainframe = Tkinter.Frame(parent)
self.mainframe.pack(fill = 'both', expand = 1)
self.notebook = Pmw.NoteBook(self.mainframe)
buttonbox = Pmw.ButtonBox(self.mainframe)
buttonbox.pack(side = 'bottom', fill = 'x')
buttonbox.add('Add Tab', command = self.insertpage)
self.notebook.pack(fill = 'both', expand = 1, padx = 5, pady = 5)
def insertpage(self):
# Create a new Tab
self.pageCounter = self.pageCounter + 1
pageName = 'Tab%d' % (self.pageCounter)
page = self.notebook.insert(pageName)
self.showPageContent()
def showPageContent(self):
# This function should contain the content for all new Tabs
tabContentExample = Tkinter.Label(page, text="This is the Tab Content I want to repeat\n")
tabContentExample.pack()
# Create demo in root window for testing.
if __name__ == '__main__':
root = Tkinter.Tk()
Pmw.initialise(root)
widget = Demo(root)
root.mainloop()
Can anyone give me a pointer to the solution?
showPageContentis using the variablepagewhich is undefined. You define it locally in another method, but it isn’t known to theshowPageContentmethod. You either need to useself.page, or passpagetoshowPageContent: