TypeError:'NoneType'对象不可调用,tkinter错误

0 投票
1 回答
3478 浏览
提问于 2025-04-18 07:01

我正在尝试制作一个程序,让它在按下按钮时播放声音。但是我在调用一个函数时遇到了问题。我想做的是点击低音C(或者其他音符)按钮,然后它能跳转到低音C(或者对应音符的函数),接着再调用Launch函数来启动声音。可是,它却给我报了'Nonetype'的错误。我不知道哪里出了问题。我试过把this.Launch()换成this.Launch,但这样就不执行Launch函数了。我也试过this.Launch(this),但还是不行。有没有人能帮帮我?

from tkinter import *
import winsound
import msvcrt as m

class Application(Frame):
    def __init__(this, master):
        Frame.__init__(this, master)
        this.grid()
        this.create()


    def create(this):
        test = m.kbhit()
        if test == True:
            print('test is true')
        this.sound1 = IntVar()
        this.dir = Label(this, text = "Click a button to play a sound")
        this.dir.grid(row = 1, column = 0, columnspan = 5, sticky = W)

        #Create buttons for Notes
        this.LowC = Button(this,
                           text = "Low C",
                           command = this.LowC,
                           ).grid()
        this.D = Button(this,
                           text = "D",
                           command = this.D,
                           ).grid()
        this.E = Button(this,
                           text = "E",
                           command = this.E,
                           ).grid()
        this.F = Button(this,
                           text = "F",
                           command = this.F,
                           ).grid()
        this.G = Button(this,
                           text = "G",
                           command = this.G,
                           ).grid()

        #create launch button
        this.Launch = Button(this,
                           text = "Launch",
                           command = this.Launch,
                           ).grid()

        #create sound length slider
        this.TIME = Scale(this,
                          orient = HORIZONTAL,
                          length = 400,
                          width = 20,
                          sliderlength = 10,
                          from_=0,
                          to = 5000,
                          tickinterval = 500,
                          variable = this.sound1 
                          ).grid()
        #Keypress



    #create freq conversion
    def LowC(this):
        this.freq = 262
        print(this.freq)
        this.Launch()


    def D(this):
        this.freq = 294
        print(this.freq)

    def E(this):
        this.freq = 330
        print(this.freq)

    def F(this):
        this.freq = 349
        print(this.freq)

    def G(this):
        this.freq = 392
        print(this.freq)

    #initiate beep
    def Launch(this):
        winsound.Beep(this.freq, this.sound1.get())
        print('Tada!')
        return


base = Tk()
base.title("Basic Program Outline")
base.geometry("500x500")

app = Application(base)

base.mainloop()

1 个回答

4

你有一个属性和一个方法都叫 LowC

this.LowC = Button(this,
                   text = "Low C",
                   command = this.LowC,
                   ).grid()
...

#create freq conversion
def LowC(this):
    this.freq = 262
    print(this.freq)
    this.Launch()

你应该把其中一个改个名字。

顺便说一下,如果你写 self.some_name = Button(args).grid(),那么 self.some_name 会变成 None,因为你把 grid 的结果赋值给了这个变量,而不是你想要的 Button 实例。

撰写回答