2012-07-21 47 views
0

我在寫一個使用Tkinter和Pmw的小應用程序。使用Pmw NoteBook類有一個界面,允許用戶爲輸入創建新的選項卡。在用Pmw筆記本創建的標籤中重複內容

這些選項卡中的每一個都有完全相同的內容(單選按鈕和輸入字段),所以我希望內容只用一個函數編寫。但這不起作用,我不明白爲什麼不。我在Traceback中得到一個NameError,這對我來說沒有意義,因爲已經爲整個類定義了「page」,並且我可以在其他函數定義中使用它。

<type 'exceptions.NameError'>: global name 'page' is not defined 

的什麼,我想是(基於隨PMW的NoteBook_2例子)做最簡單的工作版本:

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() 

誰能給我一個指針來解決呢?

+0

你真的需要張貼回溯 – inspectorG4dget 2012-07-21 02:01:41

+0

完整回溯是: ' 文件「/home/SimpleExample_Tabs.py」,第20行,在insertpage self.showPageContent() 文件 「/home/SimpleExample_Tabs.py」,第24行,在showPageContent tabContentExample = Tkinter.Label(頁面,文本= 「這是選項卡內容我想重複\ n」 個) <型'exceptions.NameError'>:全局名'頁面'未定義 ' – FvD 2012-07-21 02:18:19

回答

0

showPageContent正在使用未定義的變量page。您可以用另一種方法在本地定義它,但showPageContent方法不知道。你要麼需要使用self.page,或通過pageshowPageContent

page = self.notebook.insert(pageName) 
self.showPageContent(page) 
+0

謝謝布萊恩這只是poi我需要。 對於任何試圖用這樣的回答:您需要定義showPageContent爲: '高清showPageContent(個體經營,頁):' 否則會有一個關於被傳遞到該函數的參數個數投訴你的追蹤。 – FvD 2012-07-24 20:41:41