“ bool”对象在使用while

时间:2019-05-19 00:34:01

标签: python python-3.x

我正在编写一个图形应用程序,在使用数据库按下电钢琴中的一个键后,它会给出一个单词。 我正在使用PyGame,Tkinter和Sqlite。

该应用程序非常简单,即将完成, 但是我陷入了我的piano.py和frontEnd.py之间的错误。

问题是我想要一个标签,该标签可以写出我按下的最后一个键并将其放在画布上。

我知道问题与“ while True”有关,并且已经使用“ while idKey <176”对其进行了更改,但是通过此更改,我收到了“ noneType”错误。

这是我的文件piano.py中的当前代码

piano.py


import pygame
import pygame.midi
from pygame.locals import *


class backPiano():
    def funcPiano(self):
        self = backPiano
        pygame.init()


        pygame.fastevent.init()
        event_get = pygame.fastevent.get
        event_post = pygame.fastevent.post

        pygame.midi.init()

        input_id = pygame.midi.get_default_input_id()

        i = pygame.midi.Input( input_id )


        while True:
            events = event_get()

            if i.poll():
                midi_events = i.read(10)
                idKey = midi_events[0][0][0]

                if idKey == 176:
                    return False

以及我的frontEnd中的代码(仅包含有问题的函数):

frontEnd.py


from tkinter import *
from tkinter import ttk, font
import multiprocessing
import time
import os

from database import dictionary, path

from piano import backPiano

class frontEnd(Frame):

    def __init__(self, parent):

        self.backPiano = backPiano()

        def capturePiano(self):
                backPiano.funcPiano(self)
                superPiano = StringVar()
                superPiano.set(backPiano.funcPiano(self).idKey)
                labelPiano.configure(textvariable=superPiano)
                self.parent.update()


        canvasWidth = 500
        canvasHeight = 500
        w = Canvas(parent, width=canvasWidth, height=canvasHeight)
        w.place(x=monitorWidth/2,y=monitorHeight/2, anchor=CENTER)
        w.create_image(canvasWidth/2, canvasHeight/2, image=img, anchor=CENTER)


        labelPiano = Label(parent)
        labelPiano.place(x=monitorWidth/2,y=monitorHeight/2)

'superPiano.set(backPiano.funcPiano(self).idKey)'行中我尝试过:

“ superPiano.set(backPiano.idKey)”

但是因为变量在函数内部,所以不能用它来调用。

我的确切错误是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\admin\Desktop\python\frontEnd.py", line 202, in <lambda>
    command=lambda : capturePiano(self)).place(x=monitorWidth/9,y=monitorHeight/2,anchor=CENTER)
  File "C:\Users\admin\Desktop\python\frontEnd.py", line 187, in capturePiano
    superPiano.set(backPiano.funcPiano(self).idKey)
AttributeError: 'bool' object has no attribute 'idKey'

我无法上载所有代码,但错误在While True中,但是将其删除会破坏我的所有代码,因为我需要循环。

非常感谢您(如果我犯了语法错误,我们深表歉意。)

1 个答案:

答案 0 :(得分:1)

如错误消息所述:funcPiano返回一个布尔值(True),因此当您尝试使用idKey时它会失败,因为布尔值没有。