【问题标题】:NoneType object has no attribute yviewNoneType 对象没有属性 yview
【发布时间】:2014-07-05 11:35:27
【问题描述】:

首先,我想解决这个问题: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'”。我不确定为什么会这样并且一直无法找到明确的答案。感谢您的宝贵时间。

【问题讨论】:

标签: python python-2.7 tkinter


【解决方案1】:

每个 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2018-05-05
    • 2013-02-11
    • 2018-12-01
    • 2017-08-02
    • 2021-04-27
    • 2021-12-11
    相关资源
    最近更新 更多