简体   繁体   English

当我没有为对象分配任何属性时出现属性错误

[英]Getting an attribute error when I am not assigning any attribute to the object

I'm trying to make a simple colour game in Tkinter, and here is what I have coded so far:我正在尝试在 Tkinter 中制作一个简单的彩色游戏,这是我迄今为止编写的代码:

from tkinter import *

import sys

import random

window = Tk() # initialize the window
window.geometry("653x500+78+78")
window.title("Welcome to Color Game!")


c = Canvas(window, height = 500, width = 653, highlightthickness = 0)# adding bg to test if the canvas is in the window
c.pack()

instructions = """          Welcome to Color Game! In this game, you will see a word on the screen. It will be
a color, like red.

         All you have to do is type in the COLOR of the word, not the word itself. So if the word is red, but the

text color is green, then you have to type green in. You will see the word for 1.5 seconds, which is enough time
to see a word and for your brain to identify a color. Enjoy!!!

         Oh yeah, and if you enter the actual word, you actually lose points :D
"""

text = c.create_text(325, 100, text = instructions, font = ("Helvetica", 10))



def start(): #this will be where the main game happens
    second = 2000
    
    window.withdraw()# close main menu
    #Here we configure the window boi
    main = Tk()
    main.geometry("653x500+78+78")
    main.title("Enter the color of the word, not the word itself")
    main.focus_force()
    main.configure(bg = "white")

    colors = ["Red", "Blue", "Green", "Orange", "Purple", "Brown", "Yellow"]

    choose = colors.copy()# copying the list so we can alter the elements without actually muting the original list. 
    
    text_color = random.choice(choose)
    choose.remove(text_color)
    actual_color = random.choice(choose)

    text = Label(main, text = text_color, fg = actual_color, bg = "white", font = ("Helvetica", 20))
    text.place(relx = 0.5, rely = 0.5, anchor = "center")

    count_down = Label(main, text = int(int(second)/1000), fg = random.choice(colors), bg = "white", font = ("Helvetica", 20))
    count_down.place(relx = 0.5, rely = 0.25, anchor = "center")
    second -= 1000

    main.after(1000, start)
    
def nevermind():# nevermind function. Status done
    window.withdraw()
    last = Tk()
    last.geometry("350x200")
    last.title("Are you sure?")
    t = Label(last, text = "Don't Quit! Quitting is for NOOBS!")
    t.place(relx = 0.5, rely = 0.45, anchor = "center")

    lastt = Label(last, text = "Are you sure?")
    lastt.place(relx = 0.5, rely = 0.55, anchor = "center")

    def YES():
        last.destroy()
        window.deiconify()
    
    yes = Button(last, text = "Yes", command = quit)
    no = Button(last, text = "No", command = YES)
    yes.place(relx = 0.25, rely = 0.95, relwidth = 0.5, relheight = 0.1, anchor = "center")
    no.place(relx = 0.75, rely = 0.95, relwidth = 0.5, relheight = 0.1, anchor = "center")
    

#add a start button and a nevermind button ;D
start = Button(window, text = "Start Game!", bd = 0, highlightthickness = 0, bg = "powder blue", activebackground = "powder blue", command = start)
start.place(relx = 0.25, rely = 0.95, relwidth = 0.5, relheight = 0.1, anchor = "center")

never_mind = Button(window, text = "Never mind!", bd = 0, highlightthickness = 0, bg = "orange", activebackground = "orange", command = nevermind)
never_mind.place(relx = 0.75, rely = 0.95, relwidth = 0.5, relheight = 0.1, anchor = "center")

window.mainloop() #always add this :D

Here is the function specific to the error I am getting:这是特定于我得到的错误的函数:

def start(): #this will be where the main game happens
    second = 2000
    
    window.withdraw()# close main menu
    #Here we configure the window boi
    main = Tk()
    main.geometry("653x500+78+78")
    main.title("Enter the color of the word, not the word itself")
    main.focus_force()
    main.configure(bg = "white")

    colors = ["Red", "Blue", "Green", "Orange", "Purple", "Brown", "Yellow"]

    choose = colors.copy()# copying the list so we can alter the elements without actually muting the original list. 
    
    text_color = random.choice(choose)
    choose.remove(text_color)
    actual_color = random.choice(choose)

    text = Label(main, text = text_color, fg = actual_color, bg = "white", font = ("Helvetica", 20))
    text.place(relx = 0.5, rely = 0.5, anchor = "center")

    count_down = Label(main, text = int(int(second)/1000), fg = random.choice(colors), bg = "white", font = ("Helvetica", 20))
    count_down.place(relx = 0.5, rely = 0.25, anchor = "center")
    second -= 1000

    main.after(1000, start)

But I am getting the error:但我收到错误:

>   main.after(1000, start)
  File "C:\Users\offcampus\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 611, in after
    callit.__name__ = func.__name__
AttributeError: 'Button' object has no attribute '__name__'

I have looked at this question , but I am honestly confused because I am not assigning any attribute whatsoever to the Button class.我看过这个问题,但老实说我很困惑,因为我没有为Button类分配任何属性。

So what is going on here?那么这里发生了什么?

You're reusing the name start here:你正在重用这个名字, start这里start

start = Button(window, text = "Start Game!", bd = 0, highlightthickness = 0, bg = "powder blue", activebackground = "powder blue", command = start)

So when you call main.after(1000, start) it's trying to use that button, but the second argument to main.after() needs to be a function.因此,当您调用main.after(1000, start)它会尝试使用该按钮,但是main.after()的第二个参数需要是一个函数。

Don't use the same name for a variable and a function -- rename the variable used for the Start Game button, or rename the function.不要对变量和函数使用相同的名称——重命名用于“开始游戏”按钮的变量,或重命名函数。

暂无
暂无

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

相关问题 我收到属性错误为'int' object has no attribute 'to' - I am getting Attribute error as 'int' object has no attribute 'to' Tkinter:按下按钮时调用 function 但出现属性错误“应用程序”object 没有属性“嗨” - Tkinter: Calling function when button is pressed but i am getting attribute error 'Application' object has no attribute 'hi' 为什么我在字符串 object 上收到属性错误? - Why am I getting an Attribute error on a string object? 我收到错误消息:“类型”对象没有属性“ __getitem__” - I am getting an error : 'type' object has no attribute '__getitem__' 我收到一个错误:AttributeError: 'str' object has no attribute 'isfloat' - I am getting an error : AttributeError: 'str' object has no attribute 'isfloat' 我收到错误“响应”对象没有属性“ fromstring” - I am getting the error 'Response' object has no attribute 'fromstring' 我收到错误,因为列表对象没有属性“加入” - I am getting an error as list object has no attribute 'join' 为什么我收到此错误“WSGIRequest”object 没有属性“kwargs”? - Why am I getting this error 'WSGIRequest' object has no attribute 'kwargs'? 为什么会出现错误:“ int”对象没有属性“ create”? - Why am I getting the error: 'int' object has no attribute 'create'? 为什么我收到此错误:对象没有属性“ astype” - Why I am getting this error: object has no attribute 'astype'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM