【发布时间】:2020-09-04 12:16:27
【问题描述】:
我正在尝试在 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
这是特定于我得到的错误的函数:
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)
但我得到了错误:
> 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__'
我看过this question,但我真的很困惑,因为我没有为Button 类分配任何任何属性。
那么这里发生了什么?
【问题讨论】:
-
发生错误是因为,作为实现
.after逻辑的一部分,Tkinter 需要分配一个您的Button没有的属性。 真正的线索是,您没想到代码中提到的任何东西都是Button,但其中之一(特别是start)是。 -
但是是的,如果您使用
.after递归地创建游戏循环,则您不应该在 inside 该循环中创建您的 Tk 实例。 -
@HenryYik 我现在明白这个问题了。
-
@KarlKnechtel 是的,我明白了,我已经修复了一些代码。
标签: python python-3.x tkinter