NoneType对象没有属性yvi

2024-09-30 16:20:16 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我想让它远离我意识到的这个问题: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()

当我尝试运行这个时,我得到一个错误消息,在scroll1y和scroll2y滚动条上“NoneType”对象没有属性“yview”。我不确定这是为什么,也一直无法找到一个明确的答案。谢谢你抽出时间。在


Tags: thetextfalsemaintkinterwindowwidthone
1条回答
网友
1楼 · 发布于 2024-09-30 16:20:16

每个Tkinter小部件的gridpack和{}方法正常工作(它们总是返回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()

相关问题 更多 >