[英]Nest a function
我正在嘗試使用tkinter在python中開發游戲。
為此,我需要使用嵌套函數。 我想我已正確格式化了調用,但錯誤代碼是:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Chris\AppData\Local\Programs\Python\Python36-32\Lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:/Users/Chris/Desktop/Paul's stuff/Paul/CpmpProject/question screen.py", line 56, in display
answer1 = Button(self.master, text=output[0], command= Buttons.display.check())
AttributeError: 'function' object has no attribute 'check'
我的代碼如下。 我是tkinter的新手,所以如果可能的話,基本的解釋是最好的。
from tkinter import *
from pygame.locals import *
class Buttons:
def __init__(self, master):
self.master = master
self.frame = Frame(self.master)
self.b1 = Button(self.master, text="Button1", command=self.display)
self.b2 = Button(self.master, text="Button2", command=self.new_window)
self.b1.pack()
self.b2.pack()
self.frame.pack()
def display(self):
def check():
if player_answer == answers[s][0] or player_answer == answers[s][1]:
score += 1
return score
else:
print("Wrong")
return score
import random
import pygame
from random import shuffle
import sys
questions = ["What species of bird is also a nickname for New Zealand?",
"Which Twins can you play as in Assassin's Creed Syndicate?",
"Which year was 'Killing In The Name' Christmas Number one?"]
answers = [["kiwi", "Kiwi", "Falcon", "Sparrow", "Crow"], ["frye", "Frye", "Bank", "Green", "Bundy"],
["2009", "2009",
"1999", "1993",
"2004"]]
# I had to do it in two separate lists as it's easier to work with later on
# Also I made the correct answers non-case sensitive to make it easier to test.
r = len(questions)
score = 0
s = random.randrange(0, r, 1)
# This generates a random number within the range of how many questions there are
# and then prints out that question
w = Label(text=questions[s])
w.pack()
pygame.init()
self.FPS = 60
self.fps_clock = pygame.time.Clock()
self.surface = pygame.display.set_mode((640, 480))
list = answers[s]
output = []
for i in range(1, 5):
output.append(answers[s][i])
shuffle(output)
answer1 = Button(self.master, text=output[0], command= Buttons.display.check())
answer2 = Button(self.master, text=output[1], command= Buttons.display.check())
answer3 = Button(self.master, text=output[2], command= Buttons.display.check())
answer4 = Button(self.master, text=output[2], command= Buttons.display.check())
# this takes the answers that correspond with the randomly generated question and shuffles the answers
# I did this as otherwise, the answer would always be the first answer to appear and the player could exploit this
answer1.pack()
answer2.pack()
answer3.pack()
answer4.pack()
player_answer = input()
while True:
pygame.display.update()
self.fps_clock.tick(self.FPS)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
我真的需要幫助,否則我不會在公共平台上詢問。
你不想要Buttons.display.check
,它將獲得Buttons.display
的check
屬性。 你只想要check
,因為它已經在范圍內了。
另外,不要打電話; 通過check
,而不是check()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.