相关疑难解决方法(0)

不明白这个AttributeError的含义

我不断收到错误消息

AttributeError: 'NoneType' object has no attribute 'something'
Run Code Online (Sandbox Code Playgroud)

我所拥有的代码太长了,无法在此发布,但我想知道是否有人可以给出一般情况会导致这种情况的要点AttributeError,以及这NoneType应该是什么意思?

通常,您会收到代码出错的某个对象的名称,但由于它给了AttributeError我,我不确定如何缩小正在发生的事情,除了行号.

python attributeerror nonetype

263
推荐指数
6
解决办法
78万
查看次数

显示和隐藏小部件

如何在Tkinter中显示和隐藏小部件?我想要一个输入框,但不要一直显示它.有人能告诉我在tkinter中显示和隐藏条目小部件和其他小部件的功能吗?我希望能够在没有多个帧的情况下做到这一点.

python user-interface tkinter

19
推荐指数
2
解决办法
4万
查看次数

如何使用tkinter更改按钮颜色

我一直收到以下错误:AttributeError:'NoneType'对象没有属性'configure'

# create color button
self.button = Button(self,
                     text = "Click Me",
                     command = self.color_change,
                     bg = "blue"
                    ).grid(row = 2, column = 2, sticky = W)

def color_change(self):
    """Changes the button's color"""

    self.button.configure(bg = "red")
Run Code Online (Sandbox Code Playgroud)

python tkinter button

10
推荐指数
1
解决办法
8万
查看次数

'NoneType'对象没有属性'config'

我在这里尝试做的是将图像添加到我拥有的按钮,然后基于点击或悬停更改图像.我所遵循的所有示例都使用该.config()方法.

对于我的生活,我无法弄清楚为什么它不知道按钮对象是什么.有趣的是,如果我修改Button定义行以包含图像选项,一切都很好.但是,有了它,似乎我不能使用它来修改它.config()

PlayUp = PhotoImage(file=currentdir+'\Up_image.gif')
PlayDown = PhotoImage(file=currentdir+'\Down_image.gif')
#Functions
def playButton():
    pButton.config(image=PlayDown)
pButton = Button(root, text="Play", command="playButton").grid(row=1)
pButton.config(image=PlayUp)
Run Code Online (Sandbox Code Playgroud)

python image button nonetype

7
推荐指数
1
解决办法
2万
查看次数

一起使用asyncio和Tkinter而不冻结GUI

我想asynciotkinterGUI 结合使用.我是新手asyncio,我对它的理解不是很详细.单击第一个按钮时,此示例启动10个任务.任务只是模拟工作sleep()几秒钟.

Python的示例代码运行良好3.6.4rc1.但问题是GUI被冻结了.当我按下第一个按钮并启动10个asyncio任务时,我无法按下GUI中的第二个按钮,直到完成所有任务.GUI永远不应该冻结 - 这是我的目标.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *
from tkinter import messagebox
import asyncio
import random

def do_freezed():
    """ Button-Event-Handler to see if a button on GUI works. """
    messagebox.showinfo(message='Tkinter is reacting.')

def do_tasks():
    """ Button-Event-Handler starting the asyncio part. """
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(do_urls())
    finally:
        loop.close()

async def one_url(url):
    """ One task. """
    sec = random.randint(1, 15)
    await …
Run Code Online (Sandbox Code Playgroud)

python user-interface asynchronous tkinter python-asyncio

7
推荐指数
3
解决办法
6076
查看次数

在`x = xy()`之后,为什么`x`变成了`None`而不是被修改(可能导致“AttributeError:'NoneType'对象没有属性”)?

如果您的问题作为此问题的重复项而被关闭,那是因为您有一些通用形式的代码

x = X()
# later...
x = x.y()
# or:
x.y().z()
Run Code Online (Sandbox Code Playgroud)

其中X是某种类型,它提供了y旨在z变异修改)对象(X类型的实例)的方法。这可以适用于:

  • 可变的内置类型,例如listdictsetbytearray
  • 标准库(尤其是 Tkinter 小部件)或第三方库提供的类。

这种形式的代码很常见,但并不总是错误的。问题的明显迹象是:

  • x.y().z()一样,会引发异常AttributeError: 'NoneType' object has no attribute 'z'

  • 有了x = x.y(),x就变成None, 而不是被修改的对象。这可能会被后来的错误结果发现,或者被像上面这样的异常(x.z()稍后尝试时)发现。

Stack Overflow 上有大量关于这个问题的现有问题,所有这些问题实际上都是同一个问题。之前甚至有多次尝试在特定上下文中涵盖同一问题的规范。然而,理解问题并不需要上下文,因此这里尝试一般性地回答:

代码有什么问题吗?为什么这些方法会这样,我们如何解决这个问题?


另请注意,当尝试使用 alambda或列表理解)来产生副作用时,会出现类似的问题。

同样明显的问题可能是由因其他原因返回的方法引起的None …

python attributeerror command-query-separation nonetype

7
推荐指数
1
解决办法
577
查看次数

为什么我的Tkinter小部件存储为None?

我把按钮放到一个阵列中但是当我打电话给他们时他们不在那里.如果我打印出阵列,我得到:

{0: None, 1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, ...}
Run Code Online (Sandbox Code Playgroud)

我只是不知道我做错了什么.

from tkinter import *

def main():
    pass

if __name__ == '__main__':
    main()

b={}

app = Tk()
app.grid()

f = Frame(app, bg = "orange", width = 500, height = 500)
f.pack(side=BOTTOM, expand = 1)


def color(x):
   b[x].configure(bg="red") # Error 'NoneType' object has no attribute 'configure'
   print(b) # 0: None, 1: None, 2: None, 3: None, 4: None, 5:.... ect


def genABC():
    for …
Run Code Online (Sandbox Code Playgroud)

python dictionary tkinter button

6
推荐指数
1
解决办法
1910
查看次数

TK python 检查按钮 RTL

我有一个复选按钮:

from tkinter import *
master = Tk()
Checkbutton(master, text="Here...").grid(row=0, sticky=W)
mainloop()
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

在此输入图像描述

我尝试将复选按钮移到另一侧(以支持 RTL 语言),所以它会像:

Here...[]

我知道我可以在复选按钮旁边绘制一个标签,但这样单击文本不会影响复选按钮。

我该怎么做?

python tk-toolkit tkinter right-to-left tkinter.checkbutton

6
推荐指数
1
解决办法
330
查看次数

为什么返回 `None` 而不是 tkinter.Entry 对象?

我是 python 的新手,四处闲逛,我注意到了这一点:

from tkinter import *
def test1():
    root = Tk()
    txtTest1 = Entry(root).place(x=10, y=10)
    print(locals())
def test2():
    root = Tk()
    txtTest2 = Entry(root)
    txtTest2.place(x=10, y=10)#difference is this line
    print(locals())
test1()
test2()
Run Code Online (Sandbox Code Playgroud)

输出包括:

'txtTest1': None

'txtTest2': <tkinter.Entry object at 0x00EADD70>

为什么 test1 有一个None而不是<tkinter.Entry object at ...

我正在使用 python 3.2 和 PyScripter。

python tkinter python-3.2

5
推荐指数
1
解决办法
2158
查看次数

Python Tkinter:AttributeError:Checkbutton 实例没有属性“get”

我是 Tkinter 的新手。我正在尝试创建一个具有两个旋转框和两个复选框的 GUI,当用户单击特定按钮(“开始”)时,将打印其值。到目前为止,这是我的代码:

from Tkinter import *
from tkFileDialog import askopenfilename
from PIL import Image

#create TK frame
root = Tk()
#identify the dimensions of the TK frame
root.geometry("360x150")
#title the TK frame
root.title("Literature Online API")

#create a function that will return the filepath for a file provided by the user
def selectfile():
    user_defined_filepath['filename'] = askopenfilename(filetypes=[("Text","*.txt")]) # user_defined_filepath['filename'] may now be accessed in the global scope.

#create a function that will allow the "start" button to begin further processes …
Run Code Online (Sandbox Code Playgroud)

python checkbox user-interface tkinter

5
推荐指数
1
解决办法
1万
查看次数