2010-09-07 121 views
1

我对Python很陌生,跟随Dive进入Python 2并希望涉足一些Tkinter编程。我试着制作一个需要3组词汇的小程序,并将3组中的每个词汇组合为网站的关键词。当我运行该脚本,将出现GUI预期,但是当我点击创建按钮的组合,我得到以下错误Python Tkinter Text Widget .get方法错误

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__ 
    return self.func(*args) 
    File "combomaker.py", line 34, in makeCombinations 
    primaryraw = primaryKeyWordsBox.get() 
AttributeError: 'NoneType' object has no attribute 'get' 

我想要的代码来解决

#!/usr/bin/env python 
from Tkinter import * 

primaryKeyWordsLabel = None 
primaryKeyWordsBox = None 
secondaryKeyWordsLabel = None 
secondaryKeyWordsBox = None 
tertiaryKeyWordsLabel = None 
tertiaryKeyWordsBox = None 

class Application(Frame): 
def __init__(self, master=None, padx = 10, pady= 10): 
    Frame.__init__(self, master) 
    self.grid() 
    self.createWidgets() 

def createWidgets(self): 
    self.primaryKeyWordsLabel = LabelFrame(text="Primary Key Words", padx=10, pady=10) 
    self.primaryKeyWordsLabel.grid() 
    self.primaryKeyWordsBox = Text(primaryKeyWordsLabel, autoseparators=True, height=5, undo=True) 
    self.primaryKeyWordsBox.grid() 
    self.secondaryKeyWordsLabel = LabelFrame(text="Secondary Key Words", padx=10, pady=10) 
    self.secondaryKeyWordsLabel.grid() 
    self.secondaryKeyWordsBox = Text(secondaryKeyWordsLabel, autoseparators=True, height=5, undo=True) 
    self.secondaryKeyWordsBox.grid() 
    self.tertiaryKeyWordsLabel = LabelFrame(text="Tertiary Key Words", padx=10, pady=10) 
    self.tertiaryKeyWordsLabel.grid() 
    self.tertiaryKeyWordsBox = Text(tertiaryKeyWordsLabel, autoseparators=True, height=5, undo=True) 
    self.tertiaryKeyWordsBox.grid() 
    self.goButton = Button(text="Create Combinations", command=makeCombinations) 
    self.goButton.grid() 

def makeCombinations(): 
    primaryraw = primaryKeyWordsBox.get() 
    primary = primaryraw.split(', ') 
    secondaryraw = secondaryKeyWordsBox.get() 
    secondary = secondaryraw.split(', ') 
    tertiaryraw = tertiaryKeyWordsBox.get() 
    tertiary = tertiaryraw.split(', ') 
    output=[] 
    filename = "output.txt" 
    for i in range(len(primary)): 
    for j in range(len(secondary)): 
    for k in range(len(tertiary)): 
    rawcombo=str(primary[i])+" "+str(secondary[j])+" "+str(tertiary[k]) 
    output.append(rawcombo) 
    FILE = open(filename, w) 
    for combo in output: 
    FILE.write(combo+",\n") 
    FILE.close() 
app = Application()      
app.master.title("Keyword Generator") 
app.mainloop()  

我可能把自己投入GUI编程过快,这是我在任何GUI工作中的第一次尝试,但不是我第一次编程。
提前:)

回答

1

非常感谢您尝试访问

primaryKeyWordsBox 

Application之外的(免费)功能makeCombinations(..)

你可以做makeCombinations(..)Application成员通过缩进它像其他成员函数,并添加self说法:

def makeCombinations(self): 

您应该修改makeCombinations(..)到按键绑定:

...,command = self.makeCombinations) 

然后,当您尝试访问此课程的成员时,您必须添加self.

primaryraw = self.primaryKeyWordsBox.get(1.0,END) 
... 
secondaryraw = self.secondaryKeyWordsBox.get(1.0,END) 
... 
tertiaryraw = self.tertiaryKeyWordsBox.get(1.0,END) 

(我发现的例子如何使用gethere)。

如果你想打开一个只写一个文件,你应该做的:的

FILE = open(filename, "w") 

代替

FILE = open(filename, w) 
+0

谢谢安德烈Holzner的,这是完全固定我的问题。我很高兴我现在有一个可用的GUI应用程序,尽管它很简单:) – 2010-09-08 06:44:32