[英]Python Tkinter Text Widget .get method error
我对Python还是很陌生,有点喜欢Dive into Python 2,并且想涉足一些Tkinter编程。 我试图制作一个小程序,该程序需要3个单词集,并在3个单词集中组合每个单词,以构成网站的关键字。 运行脚本时,GUI会按预期显示,但是单击“创建组合”按钮时出现以下错误
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工作中的第一次尝试,但不是我的第一次编程。
提前谢谢了 :)
您正在尝试访问
primaryKeyWordsBox
(免费)函数makeCombinations(..)
类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)
(我发现示例如何使用get
这里 )。
如果要打开文件进行写入,则应该执行以下操作:
FILE = open(filename, "w")
代替
FILE = open(filename, w)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.