Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 嵌套函数_Python_Tkinter_Nested - Fatal编程技术网

Python 嵌套函数

Python 嵌套函数,python,tkinter,nested,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

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

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

您不需要
按钮。display.check
,这将获得
按钮的
check
属性。display
。您只需要检查,因为它已经在范围内


还有,不要叫它;传递
check
,而不是
check()

第一个错误不要调用函数,将
按钮更改为
按钮。display.check()
改为
按钮。display.check>,python将在按下按钮时调用函数,您不需要在函数中嵌套函数,您可以将其添加到类下并调用它