问题描述
我正在尝试在DISABLED
Entry
小部件上放置滚动条.但是,它总是出现错误AttributeError: 'NoneType' object has no attribute 'xview'
.是因为没有返回值,还是无论是否返回值,窗口小部件都应该出现?
I'm trying to place a scrollbar on a DISABLED
Entry
widget. However it keeps coming up with the error 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()
推荐答案
问题是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)
…您要将decimalView
设置为None
.这就是为什么错误消息告诉您在None
上找不到xview
属性的原因.
… you're setting decimalView
to None
. Which is why the error message tells you that it can't find an xview
attribute on None
.
这不是Tkinter的怪癖;从list.sort
到file.write
,几乎Python中几乎所有以任何方式使对象发生变异的方法都返回None
(或者有时返回一些其他有用的值,但从不返回self
).
And this isn't some weird quirk of 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
.
只需将其写在两行上:构造Entry
并将其分配给decimalView
,然后将其分配给grid
.
Just write it on two lines: construct the Entry
and assign it to decimalView
, and then grid
it.
除了具有工作代码的次要好处之外,您还将拥有更具可读性的代码,该代码不会在StackOverflow或大多数文本编辑器上滚动到屏幕的右侧.
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.
这篇关于Python-tkinter'AttributeError:'NoneType'对象没有属性'xview'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!