简体   繁体   English

Python-tkinter'AttributeError:'NoneType'对象没有属性'xview'

[英]Python - tkinter 'AttributeError: 'NoneType' object has no attribute 'xview''

I'm trying to place a scrollbar on a DISABLED Entry widget. 我正在尝试在“ DISABLED Entry窗口小部件上放置滚动条。 However it keeps coming up with the error AttributeError: 'NoneType' object has no attribute 'xview' . 但是,它总是出现错误AttributeError: 'NoneType' object has no attribute 'xview' Is it because no value is being returned or should the widget appear regardless of whether or not a value is being returned? 是因为没有返回值,还是无论是否返回值,都应该显示小部件?

Below is the code for my program; 下面是我程序的代码; I have commented out the code for the scrollbar: 我已注释掉滚动条的代码:

from tkinter import *
from tkinter import ttk

def calculate(*args):
    try:
        value = int(binary.get(), 2)
    except ValueError:
        pass

    decimal.set(value)

root = Tk()
root.title("Binary to Decimal Converter")
root.wm_iconbitmap("python-xxl.ico")

mainframe = ttk.Frame(root, padding = "3 3 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)

binary = StringVar()
decimal = StringVar()

binary_entry = ttk.Entry(mainframe, width = 30, textvariable = binary)
binary_entry.grid(column = 2, row = 1, sticky = (W, E))

decimalView = ttk.Entry(mainframe, state = DISABLED, background = "gray99", width = 30, textvariable = decimal).grid(column = 2, row = 2, sticky = W)
"""scrollbar = Scrollbar(mainframe, orient = HORIZONTAL, command = decimalView.xview)
scrollbar.grid(column = 2, row = 3, sticky = (N, S, E, W))
decimalView.config(command = scrollbar.set)"""
ttk.Button(mainframe, text = "Calculate", command = calculate).grid(column = 3, row = 3, sticky = W)

ttk.Label(mainframe, text = "Binary").grid(column = 3, row = 1, sticky = W)
ttk.Label(mainframe, text = "Decimal").grid(column = 3, row = 2, sticky = W)

for child in mainframe.winfo_children():
    child.grid_configure(padx = 5, pady = 5)

binary_entry.focus()
root.bind("<Return>", calculate)

root.mainloop()

The problem is that grid returns None , not self . 问题在于grid返回None而不是self

So, when you do this: 因此,当您这样做时:

decimalView = ttk.Entry(mainframe, state = DISABLED, background = "gray99", width = 30, textvariable = decimal).grid(column = 2, row = 2, sticky = W)

… you're setting decimalView to None . …您正在将decimalView设置为None Which is why the error message tells you that it can't find an xview attribute on None . 这就是为什么错误消息告诉您无法在None上找到xview属性的原因。

And this isn't some weird quirk of Tkinter; 这不是Tkinter的怪癖。 almost every method in Python that mutates an object in any way returns None (or, sometimes, some other useful value—but never self ), from list.sort to file.write . list.sortfile.write ,几乎Python中以任何方式进行对象变异的每个方法都返回None (或者有时返回一些其他有用的值,但从不返回self )。

Just write it on two lines: construct the Entry and assign it to decimalView , and then grid it. 只需将其写在两行上:构造Entry并将其分配给decimalView ,然后对其进行grid

Besides the minor benefit of having working code, you'll also have more readable code, that doesn't scroll off the right side of the screen on StackOverflow or most text editors. 除了拥有工作代码的次要好处外,您还将拥有更具可读性的代码,这些代码不会在StackOverflow或大多数文本编辑器上滚动到屏幕的右侧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python TKInter:属性错误:&#39;NoneType&#39;对象没有属性&#39;create_image&#39; - Python TKInter: AttributeError: 'NoneType' object has no attribute 'create_image' Python,AttributeError:“ NoneType”对象没有属性“ show” - Python, AttributeError: 'NoneType' object has no attribute 'show' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;findNext&#39; - Python: AttributeError: 'NoneType' object has no attribute 'findNext' Python AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;fileno&#39; - Python AttributeError: 'NoneType' object has no attribute 'fileno' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;groups&#39; - Python: AttributeError: 'NoneType' object has no attribute 'groups' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;type&#39; - Python: AttributeError: 'NoneType' object has no attribute 'type' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;split&#39; - Python:AttributeError: 'NoneType' object has no attribute 'split' Python - AttributeError:&#39;NoneType&#39;对象没有属性&#39;cursor&#39; - Python - AttributeError: 'NoneType' object has no attribute 'cursor' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;start&#39; - Python: AttributeError: 'NoneType' object has no attribute 'start' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;append&#39; - Python : AttributeError: 'NoneType' object has no attribute 'append'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM