首先,我想让我知道这个问题的方式:Python - tkinter 'AttributeError: 'NoneType' object has no attribute 'xview''
然而,在阅读完本文后,我仍然对这个问题感到困惑。我有一个使用Tkinter的程序。在其中,有两个文本框,用户可以键入文本。我希望那些盒子可以滚动。但是,我这样做有问题。这是我的代码:
from Tkinter import *
def main():
window = Tk()
window.title("TexComp")
window.geometry("500x500")
window.resizable(height=FALSE,width=FALSE)
windowBackground = '#E3DCA8'
window.configure(bg=windowBackground)
instruction = Label(text="Type or paste your text into one box,\nthen paste the text you want to compare it too\ninto the other one.", bg=windowBackground).place(x=115, y=10)
text1 = Text(width=25).pack(side=LEFT)
text2 = Text(width=25).pack(side=RIGHT)
scroll1y=Scrollbar(window, command=text1.yview).pack(side=LEFT, fill=Y, pady=65)
scroll2y=Scrollbar(window, command=text2.yview).pack(side=RIGHT, fill=Y, pady=65)
mainloop()
if __name__ == '__main__':
main()
当我尝试运行此操作时,我收到错误消息"' NoneType'对象没有属性' yview'"在scroll1y和scroll2y滚动条上。我不确定为什么会这样,并且无法找到明确的答案。谢谢你的时间。
答案 0 :(得分:2)
每个Tkinter小部件的grid
,pack
和place
方法就地工作(它们始终返回None
)。意思是,你需要按照自己的方式调用它们:
from Tkinter import *
def main():
window = Tk()
window.title("TexComp")
window.geometry("500x500")
window.resizable(height=FALSE,width=FALSE)
windowBackground = '#E3DCA8'
window.configure(bg=windowBackground)
instruction = Label(text="Type or paste your text into one box,\nthen paste the text you want to compare it too\ninto the other one.", bg=windowBackground)
instruction.place(x=115, y=10)
text1 = Text(width=25)
text1.pack(side=LEFT)
text2 = Text(width=25)
text2.pack(side=RIGHT)
scroll1y=Scrollbar(window, command=text1.yview)
scroll1y.pack(side=LEFT, fill=Y, pady=65)
scroll2y=Scrollbar(window, command=text2.yview)
scroll2y.pack(side=RIGHT, fill=Y, pady=65)
mainloop()
if __name__ == '__main__':
main()