2013-09-29 160 views
1

我是Tkinter的新手。我正在嘗試創建一個GUI,其中有兩個旋轉框和兩個複選框,當用戶單擊某個特定按鈕(「開始」)時,這些複選框的值將被打印出來。這是我到目前爲止的代碼:Python Tkinter:AttributeError:Checkbutton實例沒有屬性'get'

from Tkinter import * 
from tkFileDialog import askopenfilename 
from PIL import Image 

#create TK frame 
root = Tk() 
#identify the dimensions of the TK frame 
root.geometry("360x150") 
#title the TK frame 
root.title("Literature Online API") 

#create a function that will return the filepath for a file provided by the user 
def selectfile(): 
    user_defined_filepath['filename'] = askopenfilename(filetypes=[("Text","*.txt")]) # user_defined_filepath['filename'] may now be accessed in the global scope. 

#create a function that will allow the "start" button to begin further processes 
def startapi(event = "<Button>"): 
    print windowlengthspinbox.get() 
    print slideintervalspinbox.get() 
    print fuzzyspellingbutton.get() 
    print lemmatizedsearchbutton.get() 

#create variables for the checkbuttons -- default = 0, checked = 1 
fuzzyspellingvariable = IntVar() 
lemmatizedsearchvariable = IntVar() 

#create a caption that will appear as the first line of the grid 
firstlinelabel = Label(root, text = "Please select any desired search options:") 
firstlinelabel.grid(row = 0, column = 0, sticky = W) 

#create a button that allows users to employ Literature Online's fuzzy spelling feature. Add the object.grid() method on new line because appending .grid() to the line in which one defines object causes Python to give the object attribute "NoneType." http://stackoverflow.com/questions/1101750/python-tkinter-attributeerror-nonetype-object-has-no-attribute-get 
fuzzyspellingbutton = Checkbutton(root, text="Fuzzy Spelling", variable=fuzzyspellingvariable) 
fuzzyspellingbutton.grid(row = 1, column = 0, sticky = W) 

#create a button that allows users to employ Literature Online's lemmatized search feature 
lemmatizedsearchbutton = Checkbutton(root, text="Lemmatized Search", variable=lemmatizedsearchvariable) 
lemmatizedsearchbutton.grid(row = 2, column = 0, sticky = W) 

#create a spinbox that allows users to identify desired window length 
windowlengthspinbox = Spinbox(root, from_=1, to=10) 
windowlengthspinbox.grid(row = 3, column = 1, sticky = W) 
windowlengthspinboxlabel = Label(root, text = "Please select window size") 
windowlengthspinboxlabel.grid(row = 3, column = 0, sticky = W) 

#create a spinbox that allows users to identify desired window length 
slideintervalspinbox = Spinbox(root, from_=1, to=10) 
slideintervalspinbox.grid(row = 4, column = 1, sticky = W) 
slideintervalspinboxlabel = Label(root, text = "Please select window slide interval") 
slideintervalspinboxlabel.grid(row = 4, column = 0, sticky = W) 

#create a button that allows users to find a file for analysis  
selectfilebutton = Button(root,text="Select File",command=selectfile) 
selectfilebutton.grid(row = 5, column = 0, sticky = W) 

#create a start button that allows users to submit selected parameters and file 
startbutton = Button(root, text="Start", command = startapi, width = 8) 
startbutton.grid(row = 5, column = 1, sticky = E) 
startbutton.bind("<Button>", startapi) 
#startbutton.focus() 

#instantiate the Tk window 
root.mainloop() 

當我點擊「開始」按鈕,在GUI打印spinboxes的值,然後告訴我,Checkbutton實例沒有屬性「得到」:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ 
    return self.func(*args) 
    File "groundupgui.py", line 20, in startapi 
    print fuzzyspellingbutton.get() 
AttributeError: Checkbutton instance has no attribute 'get' 

有誰知道我如何能打印複選框的值?我會很感激任何建議或見解。

回答

3

要找到Checkbutton的狀態下使用該fuzzyspellingvariable你已經連接到Checkbutton:

fuzzyspellingvariable.get() 

順便說一句,因爲checkbutton只有兩種狀態,你可以使用一個BooleanVar而不是IntVar這裏:

fuzzyspellingvariable = BooleanVar() 
+0

非常感謝,@unutbu!我認爲我們同時發佈! – duhaime

相關問題