问题描述
我正在尝试制作一个可以在按下按钮时播放声音的程序.但是我在调用函数时遇到了麻烦.我想要做的是单击 LowC(或任何其他音符)按钮,它会转到 LowC(或其各自的音符功能),然后转到功能 Launch 以启动声音.相反,它给了我Nonetype"错误.我不知道出了什么问题.我试过用这个替换 this.Launch() .启动,但是它没有执行启动功能,就像我尝试过的 this.Launch(this) 一样,但是它仍然不起作用.有什么帮助吗?
I am trying to make a program that will play sounds on a button press. But i am having a trouble calling a function. What I want to do is click on the LowC (or any other note) button and it goes to the LowC (or its respective note function) and then goes to the function Launch to initiate the sound. Instead, it gives me the 'Nonetype' error. I do not know what is wrong. I've tried replacing this.Launch() with this. Launch, but then it doesn't execute the Launch function as well as I have tried this.Launch(this), but then it still doesn't work. Any help?
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()
推荐答案
您同时拥有一个名为 LowC
的属性和方法.
You have both an attribute and a method named 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()
您应该重命名其中之一.
You should rename one of them.
顺便说一下,如果你做 self.some_name = Button(args).grid()
,那么 self.some_name
将是 None
>,因为您将 grid
的结果分配给变量,而不是您想要的 Button
实例.
By the way, if you do self.some_name = Button(args).grid()
, then self.some_name
will be None
, because you're assigning the result of grid
to the variable, not the Button
instance that you want.
这篇关于TypeError: 'NoneType' 对象不能用 tkinter 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!