簡體   English   中英

Python Tkinter文本小部件.get方法錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM