嵌套功能

时间:2018-01-11 19:26:16

标签: python tkinter nested

我正在尝试使用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()

我真的需要帮助,否则我不会在公共平台上询问。

1 个答案:

答案 0 :(得分:3)

您不希望Buttons.display.check获得check的{​​{1}}属性。你只想要Buttons.display,因为它已经在范围内了。

另外,不要打电话给它;传递check,而不是check