【问题标题】:Python Tkinter Text Widget .get method errorPython Tkinter Text Widget .get 方法错误
【发布时间】:2010-09-07 21:26:35
【问题描述】:

我对 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 工作,但不是我第一次编程。
非常感谢提前:)

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    您正在尝试访问

    primaryKeyWordsBox
    

    在(免费)函数makeCombinations(..)中的类Application之外。

    您可以像其他成员函数一样缩进 Application 并添加 self 参数,从而使 makeCombinations(..) 成为 Application 的成员:

     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)
    

    【讨论】:

    • 谢谢 Andre Holzner,这完美解决了我的问题。我真的很高兴我现在有了一个可以工作的 GUI 应用程序,尽管它很简单 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多