我正在尝试制作一个可以按下按钮播放声音的程序。但是我在调用函数时遇到了麻烦。我想要做的是点击LowC(或任何其他音符)按钮,它进入LowC(或其相应的音符功能),然后进入启动功能以启动声音。相反,它给了我'Nonetype'错误。我不知道出了什么问题。我试过用这个来替换它.Launch()。启动,但它没有执行启动功能以及我尝试过这个。启动(这个),但它仍然没有工作。有帮助吗?
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()
答案 0 :(得分: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
{1}}你想要的实例。