3

我是 Tkinter 的新手。我正在尝试创建一个具有两个旋转框和两个复选框的 GUI,当用户单击特定按钮(“开始”)时,它们的值将被打印出来。到目前为止,这是我的代码:

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 会打印旋转框的值,然后告诉我 Checkbutton 实例没有属性“get”:

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'

有谁知道我如何打印复选框的值?我将不胜感激任何建议或见解。

4

2 回答 2

4

要查找 Checkbutton 的状态,请使用fuzzyspellingvariable您附加到 Checkbutton 的 :

fuzzyspellingvariable.get()

顺便说一句,由于检查按钮只有两种状态,您可以在这里使用 BooleanVar 而不是 IntVar:

fuzzyspellingvariable = BooleanVar()
于 2013-09-29T15:41:57.693 回答
1

啊,我明白了。您需要对变量执行 .get() 方法,而不是按钮:

print fuzzyspellingvariable.get()
print lemmatizedsearchvariable.get()
于 2013-09-29T15:42:47.783 回答