Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter:AttributeError:Checkbutton实例没有属性';获取';_Python_User Interface_Checkbox_Tkinter - Fatal编程技术网

Python Tkinter:AttributeError:Checkbutton实例没有属性';获取';

Python Tkinter:AttributeError:Checkbutton实例没有属性';获取';,python,user-interface,checkbox,tkinter,Python,User Interface,Checkbox,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")

我是新来的。我试图创建一个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()

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

若要查找Checkbutton的状态,请使用附加到Checkbutton的
模糊拼写变量

fuzzyspellingvariable.get()

顺便说一下,由于checkbutton只有两种状态,您可以在此处使用BooleanVar而不是IntVar:

fuzzyspellingvariable = BooleanVar()

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

print fuzzyspellingvariable.get()
print lemmatizedsearchvariable.get()

如果我不想创建或为复选框分配新变量,该怎么办?
print fuzzyspellingvariable.get()
print lemmatizedsearchvariable.get()