简体   繁体   English

Python tkinter TypeError:类型为“ NoneType”的对象没有len()

[英]Python tkinter TypeError: object of type 'NoneType' has no len()

I have made a programm, wher you can decide in a scale, how much words you want. 我已经编写了程序,可以按比例确定想要多少个单词。 When I take 1 in the scale and then try to print it in a label, I get the Error: 当我在秤中取1,然后尝试在标签中打印时,出现错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "C:\Users\Eduard\Desktop\Zeugs\python\test.py", line 69, in ok3
label['text']=random.choice(WORDS)
File "C:\Python33\lib\random.py", line 249, in choice
i = self._randbelow(len(seq))
TypeError: object of type 'NoneType' has no len()

Here is the code: 这是代码:

import tkinter as tk
from tkinter import *
import random
from functools import partial

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container= tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames={}
        for F in (mode2, scale1):
            frame= F(container, self)
            self.frames[F]=frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(mode2)

    def show_frame(self, c):
        frame=self.frames[c]
        frame.tkraise()

class mode2(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        def antmenge(self):
            label1["text"]="Mögliche Antworten: " \
                + str(antmengen.get()) + " "

        label1=tk.Label(self, text="Mögliche Antworten: 0 Wörter", width=25)
        label1.pack()

        antmengen=IntVar()
        antmengen.set(0)

        antm=Scale(self, width=20, length=200, orient="vertical", from_=0, to=20,
        resolution=1, tickinterval=10, label="Wörter", command=antmenge(self),
        variable=antmengen)
        antm.pack()

        def abfrage():
            if antmengen.get()==1:
                button3=Button(self, text="push again", command=lambda: controller.show_frame(scale1))
                button3.pack()

        button2=tk.Button(self, text="push", command=abfrage)
        button2.pack()

class scale1(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label1=Label(self, text="Wort1")
        label1.pack()
        wort1auf=Entry(self)
        wort1auf.pack()

        label=tk.Label(self, text=" ")
        label.pack()

        a=label.configure(text=(wort1auf.get()))

        def ok3(label):
            WORDS=(a)
            label['text']=random.choice(WORDS)

        button1=tk.Button(self, text="push", command=partial(ok3, label))
        button1.pack()

if __name__== "__main__":
    app=SampleApp()
    app.mainloop()

Sorry if my English is bad. 抱歉,我的英语不好。

Okay 好的

So to start. 因此开始。

    a=label.configure(text=(wort1auf.get()))
    def ok3(label):
        WORDS=(a)
        label['text']=random.choice(WORDS)

The reason you get your error is, that a and therefore WORDS are both NONE since label.configure(...) returns None. 出现错误的原因是,因为label.configure(...)返回None,所以a和因此WORDS都为NONE。

I assume WORDS should be a list of words to choose from. 我认为WORDS应该是可供选择的单词列表。 I am not sure though what to make of your "a" did you want to populate it with the input word? 我不确定您要用输入的单词填充“ a”吗? If that is the case WORDS=(a) would work, but you have to move "a" inside "ok3" 如果是这种情况,WORDS =(a)可以使用,但是您必须在“ ok3”内部移动“ a”

    def ok3(label):
        a=label.configure(text=(wort1auf.get()))
        WORDS=(a)
        label['text']=random.choice(WORDS)

And second. 第二。 It should retrieve the value of the input. 它应该检索输入的值。

    def ok3(label):
        WORDS=(wort1auf.get()) # this will choose a random letter out of your input. 
        #WORDS=(wort1auf.get(),) # this will choose a the input word, since it is the only word inside the tuple. 
        WORDS=(a)
        label['text']=random.choice(WORDS)

hope this helps LG Daniel 希望这对LG Daniel有帮助

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

相关问题 Python-TypeError:“ NoneType”类型的对象没有len() - Python - TypeError: object of type 'NoneType' has no len() Python TypeError:“NoneType”类型的对象没有 len() - Python TypeError: object of type 'NoneType' has no len() Python TypeError: (“'NoneType' 类型的对象没有 len()” - Python TypeError: (“object of type 'NoneType' has no len()” Python:TypeError:'NoneType' 类型的对象没有 len() - Python: TypeError: object of type 'NoneType' has no len() TypeError:类型为'NoneType'的对象没有len()python - TypeError: object of type 'NoneType' has no len() python Python错误“ TypeError:类型为'NoneType'的对象没有len() - Python error "TypeError: object of type 'NoneType' has no len() Python 错误 - TypeError:“NoneType”类型的 object 没有 len() - Python error - TypeError: object of type 'NoneType' has no len() Python:TypeError:“NoneType”类型的 object 没有 len():使用 JSON 和 rncryptor - Python: TypeError: object of type 'NoneType' has no len(): Using JSON And rncryptor TypeError:“NoneType”类型的对象在应用程序部署中没有 len() Python - TypeError: object of type 'NoneType' has no len() Python on application deployment Python 错误:“TypeError:“NoneType”类型的 Object 没有 len()” - Python error: “TypeError: Object of type 'NoneType' has no len()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM